简体   繁体   English

相同的正则表达式在Java和Python中有不同的结果

[英]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));
    }

See more here . 在这里查看更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我对相同文本的 python 和 java base64 编码有不同的结果? - Why I have different results for python and java base64 encode for the same text? 使用相同的正则表达式时,Python 和 Java 正则表达式的行为不同 - Python and Java regex behavior different when using the same regex 用JAVA和Python编写的相同代码给出不同的结果(粒子过滤器) - Same code written in JAVA and Python gives different results (particle filter) index和offset在Java Regex中有不同的含义? - index and offset have different meaning in Java Regex? 为什么这些字符串比较在 java 中有不同的结果? - Why these String comparison have different results in java? 相同的正则表达式在 Java 和 Elasticsearch 中工作不同 - Same Regex works different in Java and Elasticsearch 相同的密码会导致 Java 中的不同 AES 密钥 - Same password results in different AES keys in Java 确保不同的构造函数在Java中具有相同的行为 - Ensuring different constructors have the same behaviour in Java Java和Python实现的Blowfish产生不同的结果 - java and Python implementation of Blowfish produce different results Java和python中的Whirlpool哈希给出不同的结果 - Whirlpool hash in java and in python give different results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM