import java.util.regex.*;
public class BloodPressure {
private static final String numbersOrDash = "(-|\\d{1,3})";
private static final Pattern pattern = Pattern.compile("^" + numbersOrDash + "/" + numbersOrDash + "$");
public static void main(String... args) {
for (String a : args) {
Matcher m = pattern.matcher(a);
if (m.matches()) {
System.out.println(a + " looks like a blood pressure");
}
else {
System.out.println(a + " does not look like a blood pressure");
}
}
}
}
robert:~$ java BloodPressure.java a 20/- -/20 80/120
a does not look like a blood pressure
20/- looks like a blood pressure
-/20 looks like a blood pressure
80/120 looks like a blood pressure
3条答案
按热度按时间ozxc1zmp1#
在正则表达式中使用OR表达式(
|
):(\d{1,3}|-)
-一到三位数字或连字符/
-斜线(\d{1,3}|-)
-一到三位数字或连字符这是一个working example
cqoc49vn2#
使用
|
表示“或”:我还添加了
^
和$
,分别将正则表达式锚定到输入的开头和结尾,这样,例如“abc 1/-”就不匹配了。通过一些测试值:
to94eoyn3#
这只会将
systolic
和diastolic
作为一个单元来查找。|
或\\d{1,3}
- 1至3位数字印刷品