-1

Looking for some help with a RegEx expression. I want to parse a line similar to below, capturing the integer after the letter Q:

Q232.1232 K1232.232323

would be come 232.1232 ideally in output.

The expression /(^Q)[0-9.-]* . provides me with Q232.1232 however i do not want the Q in the output.

Appreciate anyone who could assist!

jock
  • 3
  • 1

2 Answers2

1

You can use

/(?<=Q)(?:\d+(?![.\d])|\d+\.\d+(?![.\d]))/gm

demo

(?<=Q) is a positive lookbehind. It requires the match to be immediately preceded by "Q", but "Q" is not part of the match.

I've made assumptions about which strings can be matched. Those are reflected at the demo.

The regular expression can be written in free-spacing mode to make it self-documenting:

/
(?<=Q)       # match 'Q' in a positive lookbehind
(?:          # begin non-capture group
  \d+        # match representation of an integer (1+ digits)
  (?![.\d])  # do not match a period or digit (negative lookahead)
  |          # or
  \d+\.\d+   # match representation of a float
  (?![.\d])  # do not match a period or digit (negative lookahead)
)            # end non-capture group
/gmx         # global, multiline and free-spacing regex definition modes
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
0

Any group of characters enclosed in brackets will be captured. You can then use back-references to refer to these captured groups in the replacement expression.

So for your example, you would need

(^Q[0-9.-])*

back-references are normally accessed using $ or \, followed by the captured group number, $1 in your case

ds4940
  • 3,382
  • 1
  • 13
  • 20