我正在尝试使用以下规则编写正则表达式。
需要完成正则表达式匹配的文字:
Key: "employeeNo" with value "ABC12345" is already used.
规则:
1. Only the number after ABC is changing.
2. Key: "employeeNo" with value "ABC[Any Number with any length]" is
already used.
真实案例
Key: "employeeNo" with value "ABC12345" is already used.
Key: "employeeNo" with value "ABC987858547" is already used.
Key: "employeeNo" with value "ABC7" is already used.
Falsecases
key is not used
Key: "employeeNo" with value "ABCXYZYZ" is already used.
Key: employeeNo with value ABC7 is already used.
正则表达式尝试
Key: "employeeNo" with value "ABC[0-9]+" is already used.
我还想检查引号“”。
答案 0 :(得分:0)
看起来很简单,你需要逃避双引号。
String input = "Key: \"employeeNo\" with value \"ABC12345\" is already used.";
Pattern p = Pattern.compile("Key: \"employeeNo\" with value \"(ABC\\d+)\" is already used.");
Matcher m = p.matcher(input);
if (m.find()) {
System.out.println(m.group());
System.out.println(m.group(1));
}
<强>输出强>
Key: "employeeNo" with value "ABC12345" is already used.
ABC12345
如果您需要里面的 后面引用的组(组1),只需将转义的双引号括在组括号中:(\"ABC\\d+\")
。