Regex capture point surrounded by numbers

Viewed 39

I have this pretty complex Regex selection that almost works. I just seem to have a problem with understanding how non capture groups work. I've read the wiki but still can't seem to grasp how to use them properly (or this is the wrong usecase?)

\b(?:[0-9]+) ?(\.) ?(?:[0-9]+)(?! ?(uur|(u|h)))\b

If I have the following set of statements, I would like ONLY the '.' of the first 4 to be selected. ATM the Regex captures the numbers + the '.' of the first 4.

0.22
15.48
8.50
048948.00000 
05:23 uur
06.35 u
15.48u
0.55u
11.00h
5.54 uur
23: 31
08 :40 uur

I've read (and tried) to use the non capture group but it doesn't seem to help much.

Anyone have an idea on how to achieve this and care to explain a bit why it works?

Also not sure if this helps but I got a link to test the Regex I made: https://regexr.com/5dc6q

1 Answers

You can use

\b[0-9]+ ?\K\.(?= ?[0-9]+(?! ?(?:uur|[uh]))\b)

See the regex demo

Regex details

  • \b - a word boundary
  • [0-9]+ ? - one or more digits and then an optional space
  • \K - a match reset operator that discards all text matched so far
  • \. - a dot
  • (?= ?[0-9]+(?! ?(?:uur|[uh]))\b) - a location immediately followed with
    • ? - an optional space
    • [0-9]+ - one or more digits
    • (?! ?(?:uur|[uh])) - not followed with an optional space, uur or u or h
    • \b - word boundary
Related