Java 正则表达式模式不符合在线测试工具所说的
Java Regex Pattern doesn't do what the online testing tools say
我对 Java 中的正则表达式有疑问。应匹配以下
2x 1 piece
63x 9 pieces
4x 1 piece
1 piece
23 pieces
使用这个正则表达式:
((\w+)x\s)*(\w+)\s*(\w*)
众所周知,我们要对Java中的字符串进行转义。我逃脱了正则表达式并尝试使用这个:
String regex = "((\w+)x\s)*(\w+)\s*(\w*)";
现在我的问题来了:所有正则表达式的在线服务都将我的模式标记为有效,java 除外。他们没有标记可能是假的,所以我看不到我的问题。这是我试图在 Java:
中使用的代码
String regex = "((\w+)x\s)*(\w+)\s*(\w*)";
Pattern r = Pattern.compile(regex);
Matcher m = r.matcher(someClassWithMethods.text());
int multiplier=0;
int value= 0;
String supplement = "";
if (m.find( )) {
multiplier= Integer.parseInt(m.group(2));
value= Integer.parseInt(m.group(3));
supplement = m.group(4);
}
我调试了整个过程以查看发生了什么,所有变量都符合预期,但我仍然得到一个空组。这个正则表达式有什么问题?
编辑
由于这些评论,我更改了一些内容,并且我用一个额外的 if 子句捕获了我的 NumberException。现在我仍然没有得到匹配的结果。那会是什么?
这是我的新代码:
String regex = "(?:(\w+)x\s)?(\d+\s+)(pieces?)";
Pattern r = Pattern.compile(regex);
Matcher m = r.matcher(quantityCell.text());
int quantityMultiplier = 0;
int quantity = 0;
String supplement = "";
if (m.find( )) {
if(m.group(1) != null){
quantityMultiplier = Integer.parseInt(m.group(1));
}
quantity = Integer.parseInt(m.group(2));
supplement = m.group(3);
}
你的正则表达式很奇怪:
\w+
如果您只对前两个实例中的数字感兴趣,为什么还要匹配 "word character"?
((\w+)x\s)
为什么这是一个捕获组?你不想要结果。
((\w+)x\s)*
为什么要重复?您是否期待多个乘数?如果存在多个乘数,则正则表达式只会捕获最后一个乘数。
让我们试试这个:
(?:(\d+)x\s)?(\d+)\s(\w*)
由于第一次捕获是可选的,如果不存在,它将是 null
,因此您需要检查一下。
public static void main(String[] args) {
test("2x 1 piece");
test("63x 9 pieces");
test("4x 1 piece");
test("1 piece");
test("23 pieces");
}
private static void test(String input) {
String regex = "(?:(\d+)x\s)?(\d+)\s(\w*)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
int multiplier = (m.group(1) != null ? Integer.parseInt(m.group(1)) : -1);
int value = Integer.parseInt(m.group(2));
String supplement = m.group(3);
System.out.printf("%d, %d, '%s'%n", multiplier, value, supplement);
}
}
输出
2, 1, 'piece'
63, 9, 'pieces'
4, 1, 'piece'
-1, 1, 'piece'
-1, 23, 'pieces'
我对 Java 中的正则表达式有疑问。应匹配以下
2x 1 piece
63x 9 pieces
4x 1 piece
1 piece
23 pieces
使用这个正则表达式:
((\w+)x\s)*(\w+)\s*(\w*)
众所周知,我们要对Java中的字符串进行转义。我逃脱了正则表达式并尝试使用这个:
String regex = "((\w+)x\s)*(\w+)\s*(\w*)";
现在我的问题来了:所有正则表达式的在线服务都将我的模式标记为有效,java 除外。他们没有标记可能是假的,所以我看不到我的问题。这是我试图在 Java:
中使用的代码String regex = "((\w+)x\s)*(\w+)\s*(\w*)";
Pattern r = Pattern.compile(regex);
Matcher m = r.matcher(someClassWithMethods.text());
int multiplier=0;
int value= 0;
String supplement = "";
if (m.find( )) {
multiplier= Integer.parseInt(m.group(2));
value= Integer.parseInt(m.group(3));
supplement = m.group(4);
}
我调试了整个过程以查看发生了什么,所有变量都符合预期,但我仍然得到一个空组。这个正则表达式有什么问题?
编辑
由于这些评论,我更改了一些内容,并且我用一个额外的 if 子句捕获了我的 NumberException。现在我仍然没有得到匹配的结果。那会是什么? 这是我的新代码:
String regex = "(?:(\w+)x\s)?(\d+\s+)(pieces?)";
Pattern r = Pattern.compile(regex);
Matcher m = r.matcher(quantityCell.text());
int quantityMultiplier = 0;
int quantity = 0;
String supplement = "";
if (m.find( )) {
if(m.group(1) != null){
quantityMultiplier = Integer.parseInt(m.group(1));
}
quantity = Integer.parseInt(m.group(2));
supplement = m.group(3);
}
你的正则表达式很奇怪:
\w+
如果您只对前两个实例中的数字感兴趣,为什么还要匹配 "word character"?((\w+)x\s)
为什么这是一个捕获组?你不想要结果。((\w+)x\s)*
为什么要重复?您是否期待多个乘数?如果存在多个乘数,则正则表达式只会捕获最后一个乘数。
让我们试试这个:
(?:(\d+)x\s)?(\d+)\s(\w*)
由于第一次捕获是可选的,如果不存在,它将是 null
,因此您需要检查一下。
public static void main(String[] args) {
test("2x 1 piece");
test("63x 9 pieces");
test("4x 1 piece");
test("1 piece");
test("23 pieces");
}
private static void test(String input) {
String regex = "(?:(\d+)x\s)?(\d+)\s(\w*)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
int multiplier = (m.group(1) != null ? Integer.parseInt(m.group(1)) : -1);
int value = Integer.parseInt(m.group(2));
String supplement = m.group(3);
System.out.printf("%d, %d, '%s'%n", multiplier, value, supplement);
}
}
输出
2, 1, 'piece'
63, 9, 'pieces'
4, 1, 'piece'
-1, 1, 'piece'
-1, 23, 'pieces'