我有一些字符串,其中包含glucose
信息及其相应的值。例如,一个样本串是“FINGER BLOOD GLUCOSE 1562小时PP”和我有在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,
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
public Matcher find(final String text) {
return pattern.matcher(text);
}
}
// the test of the program
@Test
public void findWithCase1ShouldFindPattern() throws Exception {
assertTrue(new GlucosePattern().find(case1).find());
}
所提供的测试返回true
但是,当我使用的一些其他字符串,比方说,"Labs showed normal anion gap, glucose 278, u/a w/ 1+ ketones."
的测试失败。我相信这是由于正斜杠“/”的事实而发生的。
如何提高正则表达式正常工作?
这是导致问题的278之后的逗号。这部分正则表达式'(\\。|,)\\ d +'期望逗号后面至少有1位数字,而另一部分'(\\ s \\ d +(\\ s | $))'的正则表达式期望数字后面跟着空格。 – Wernsey
是的,这是解决的,我接受它作为答案。 – Chaklader