Reputation: 506
In a text file, I'm looking for a part of a document that contains a piece like min ISO 1133 0.2-0.35
. What I want to capture is the ranged decimal part of that piece of text (0.2-0.35
). Since there are other ranged decimal numbers, I cannot simply use a regular expression to look only for the ranged part. Till now, I could make min.*(\d+)((?:\.)?)(\d*)-(\d+)((?:\.)?)(\d*)
but the result is not correct and I'm stuck. Can anyone please help me with this?
Below, you can see the final result (yellow part):
Upvotes: 1
Views: 59
Reputation: 75840
Maybe the following would work for you?
\bmin\s.*?(\d+(?:\.\d+)?)-(\d+(?:\.\d+)?)
See the online demo
The answer is currently based on the assumption (looking at your current attempt) you'd want these ranges in seperate groups. However, if not, this answer can be swiftly transformed to capture the whole substring (or see @TheFourthBird's answer).
\b
- Match word boundary.min
- Literally match 'min'.\s
- Match a whitespace character..*?
- Match any character other than newline up to (lazy):(
- Open 1st capture group
\d+
- At least a single digit.(?:
- Open non-capturing group.
\.\d+
- Match a literal dot and at least a single digit.)?
- Close non-capturing group and make it optional.)
- Close 1st capture group.-
Match a literal hyphen.(
- Open 2nd capture group
\d+
- At least a single digit.(?:
- Open non-capturing group.
\.\d+
- Match a literal dot and at least a single digit.)?
- Close non-capturing group and make it optional.)
- Close 2nd capture group.Upvotes: 2
Reputation: 163207
You could get the decimal part matching 1+ digit in the optional part and making the quantifier non greedy. The value is in capture group 1.
\bmin [A-Z]+ [0-9]+ ([0-9]+(?:\.[0-9]+)?-[0-9](?:\.[0-9]+)?)\b
Or a bit more specific pattern
\bmin [A-Z]+ [0-9]+ ([0-9]+(?:\.[0-9]+)?-[0-9]+(?:\.[0-9]+)?)\b
Upvotes: 2