[英]Same regex have different results in Java and Python
Java Code: Java代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExpTest {
public static void main(String[] args) {
String str = "X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001";
String p = "Value = (.*?), ";
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(str);
if (matcher.find()){
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
}
}
}
Java code's output: Java代码的输出:
$ java RegExpTest
-0.525108
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 2
at java.util.regex.Matcher.group(Matcher.java:487)
at RegExpTest.main(RegExpTest.java:15)
$
Python code (in Interpreter): Python代码(在解释器中):
>>> import re
>>> re.findall("Value = (.*?), ", 'X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001;')
['-0.525108', '7.746691', '5.863008']
>>>
So, why is Java unable to match all the occurrences of the match? 那么,为什么Java无法匹配所有匹配项?
It's because a group for a java match is a capturing bracket. 这是因为java匹配的组是一个捕获括号。
Your regex only has one set of non-escaped (ie capturing) brackets, the (.*?)
. 您的正则表达式只有一组非转义(即捕获)括号(.*?)
。
Group 1 contains the value that gets matched. 组1包含要匹配的值。
There is no group 2 because there is no second set of brackets in your regex. 没有第2组 ,因为没有第二组中您正则表达式括号。
In the java example, you want to loop through all matches , and print matcher.group(1)
. 在Java例如,你想通过所有比赛圈,并打印matcher.group(1)
while ( matcher.find() ) {
System.out.println(matcher.group(1));
}
Note the while
, which will loop through all matches and tell you group 1 from each. 注意while
,它将循环浏览所有匹配项,并告诉您每个匹配项的第1组。
Java's java.util.regex.Matcher.find
attempts to find the next value that matches, not all the values that match. Java的java.util.regex.Matcher.find
尝试查找下一个匹配的值,而不是所有匹配的值。 Change if
to while
and you should get the result you are looking for. if
更改为while
,则应获得所需的结果。
...
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
...
Because you're using it wrong. 因为您使用错了。
matcher.group()
returns the values of the various capturing groups within a single match . matcher.group()
返回单个match中各个捕获组的值。 You only have one capturing group (ie one set of parentheses in your pattern). 您只有一个捕获组(即图案中的一组括号)。
matcher.find()
is the method that returns the next match, if you call it repeatedly. matcher.find()
是返回下一个匹配项的方法,如果您反复调用它。 Usually in a while loop, eg: 通常在while循环中,例如:
while (matcher.find()){
System.out.println(matcher.group(1));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.