Home > Enterprise >  how to match and show the last characters of an already regex? It is not the end of the line
how to match and show the last characters of an already regex? It is not the end of the line

Time:08-12

Lets say I have a 100 characters line. I have a REGEX matching correctly a string of lets say 25 characters in the middle (neither at the beginning nor at the end) of the line. How can I match the last 5 characters of above 25 match in the middle?

CURRENT REGEX: (dBm)\s .{7})(\b)

REAL EXAMPLE:

Lines:

Tx Output Power (dBm) -2.31 3.50 0.50 -8.20 -12.20

Rx Optical Power (avg dBm) -3.72 3.50 0.50 -14.40 -18.39

I am matching the following with my REGEX - (dBm)\s .{7})(\b)

Tx Output Power (dBm) -2.31 3.50 0.50 -8.20 -12.20

Rx Optical Power (avg dBm) -3.72 3.50 0.50 -14.40 -18.39

I am only really interested on:

Tx Output Power (dBm) -2.31 3.50 0.50 -8.20 -12.20

Rx Optical Power (avg dBm) -3.72 3.50 0.50 -14.40 -18.39

Thanks

CodePudding user response:

Regex

.*\([^\)]*\) ([^ ] ).*

Result

-2.31

-3.72

-2.31

-3.72

-2.31

-3.72

Example https://regex101.com/r/S9NO4o/1

CodePudding user response:

You could use a capture group:

\([^()]*dBm\)\s (-?\d (?:)\.\d*)\b

Explanation

  • \( Match (
  • [^()]* Match 0 chars other than ( or )
  • dBm\) Match dBm)
  • \s Match 1 whitespace chars
  • ( Capture group 1
    • -?\d (?:)\.\d* Match optional -, then 1 digits with an optional decimal part
  • ) Close group 1
  • \b A word boundary to prevent a partial word match

Regex demo

  • Related