Regex extracting specific text ignoring part of result

Viewed 44

I have a text.

The overall system blur must be smaller than 12 mrad and diameter shall be less than 20 meters.

I want to extract:

must be 12

I'm using:

^(.*?(\bmust be\b[\s*?\D]*\d+)[^$]*)$

And I get

must be smaller than 12

Any way to do this directly? Or better to try to do different groups somehow?

1 Answers

In the pattern that you tried, \D matches any char except a digit, so this [\s*?\D]* could be shortened to just \D.

This part [^$]* matches any char except a dollar sign. If you intend to match the rest of the line, you can use .* instead.


You can use 2 capturing groups instead.

^.*?\b(must be)\b\D*(\d+).*$

Regex demo

In the replacement use the 2 capturing groups like for example $1 $2

Related