Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to capture a period within an optional block

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading