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

java: regex for a positive decimal number inside a text

I have a text which contains some subtext like this:

depletion rate 1.43/second

Decimal number can vary.

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

I want to test it, so I do:

String expected = "depletion rate ^\\d*\\.\\d+|\\d+\\.\\d*$/second";
Assertions.assertThat(text).containsPattern(expected);

But this doesn’t work.

Is my regex not correct to match a positive decimal number?

>Solution :

Your regex pattern is off, and is using ^ and $ anchors in the middle of the pattern. Simply use \d+\.\d+ to match a decimal number:

String expected = "depletion rate \\d+\\.\\d+/second";
Assertions.assertThat(text).containsPattern(expected);
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