I have a text which contains some subtext like this:
depletion rate 1.43/second
Decimal number can vary.
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);