Home > Software engineering >  How to capture a period within an optional block
How to capture a period within an optional block

Time:01-14

I have the following Strings and wish to capture numbers only including decimal points.

So want to capture following type of numbers.

1
10
100
10.20

This is the regex which works for the Strings at the end.

Regex

(\$|£|$|£)(\d (?:\.\d )?)\b(?!\.)

See below where I have other Strings which all works less the following String.

$0 text $99.<sup>&#164;</sup>

This is cos the $99 is followed by a . It is not a decimal thus I don't want to capture it plus it is optional, not always gonna occur. How could I modify the regex so that I can still capture the value 99 in above String as matcher 2?

CodePudding user response:

You can use

(\$|£|&#36;|&#163;)(\d (?:\.\d )?)(?!\.?\d)

The (?!\.?\d) lookahead will only fail if there is an optional dot and then a digit immediately to the right of the current location.

  •  Tags:  
  • Related