Chaklader Chaklader - 2 months ago 8x
Java Question

Pattern with forward slash (/) is not recognized correctly in Regex

I have some some Strings with

information's with their corresponding values. For example, a sample String is "FINGER BLOOD GLUCOSE 156 two hours PP" and I have the following program in Java,

public class GlucosePattern{

// test string
private static String case1 = "FINGER BLOOD GLUCOSE 156 two hours PP";

private static final String decimalValue = "(\\d+(\\.|,)\\d+)|(\\s\\d+(\\s|$))";
private static final String glucose = "Glucose.*?";
private static final Pattern COMPILED_PATTERN = Pattern.compile(glucose+ decimalValue,

public Matcher find(final String text) {

return pattern.matcher(text);


// the test of the program
public void findWithCase1ShouldFindPattern() throws Exception {

assertTrue(new GlucosePattern().find(case1).find());

The provided test returned
but, when I used some other String, say,
"Labs showed normal anion gap, glucose 278, u/a w/ 1+ ketones."
the test is failed. I believe this is happened due to the fact of the forward slash "/".

How to improve the Regex to work on properly?


your regex is looking for a number and then a space, or a number and then a dot or a comma followed by another number. in the case where it isn't matching it is because there is not a space after the number and there is not a number after the comma.

if you want it to match, you need to update your regex to be like.. "(\\d+(\\.|,)\\d*)|(\\s\\d+(\\s|$))"