为什么Java和Perl中的正则表达式有不同的表现?

时间:2023-01-16 15:46:57

My understanding is that Java's implementation of regular expressions is based on Perl's. However, in the following example, if I execute the same regex with the same string, Java and Perl return different results.

我的理解是Java的正则表达式的实现是基于Perl的。但是,在下面的示例中,如果我使用相同的字符串执行相同的正则表达式,则Java和Perl会返回不同的结果。

Here's the Java example:

这是Java示例:

public class RegexTest {
    public static void main( String args[] ) {
        String sentence = "This is a test of regular expressions.";
        System.out.println( sentence.matches( "\\w" ) ? "Matches" : "Doesn't match" );
    }
}

This returns: Doesn't match

返回:不匹配

Here's the Perl example:

这是Perl的例子:

my $sentence = 'This is a test of regular expressions.';
print ( $sentence =~ /\w/ ? "Matches" : "Doesn't match" ) . "\n";

This returns: Matches

返回:匹配

To me, the Perl result makes sense. It looks for a match for a single word character. I don't understand why Java doesn't consider it a match. What's the reason for the difference?

对我来说,Perl结果是有道理的。它查找单个单词字符的匹配项。我不明白为什么Java不认为它是匹配的。差异的原因是什么?

2 个解决方案

#1


31  

The Java matches method is testing whether the regex matches the entire String. To test whether a regex can be found anywhere in a string, create a Matcher and use its find method.

Java matches方法正在测试正则表达式是否与整个String匹配。要测试是否可以在字符串中的任何位置找到正则表达式,请创建一个Matcher并使用其find方法。

#2


10  

Additionally, the Perl regex syntax is NOT the Java Regex Syntax.

另外,Perl正则表达式语法不是Java Regex语法。

It doesn't apply necessarily in this case, but this is a more answer to your more general question.

在这种情况下,它不一定适用,但这是对更一般性问题的更多回答。

Java has a regular expression syntax known as "PCRE", ie: Perl Compatible.

Java具有称为“PCRE”的正则表达式语法,即:Perl Compatible。

This name is however grossly misleading, because there is very very little about it which is really Perl compatible.

然而,这个名称非常具有误导性,因为它与Perl兼容并非常少。

For instance, Perl regular expressions permit executing code in the expression itself, and lots of other advanced operators, and some syntax are different in Perl as they are in other languages ( ie: many languages use \> and \< as word boundary markers, but Perl just uses '\b' )

例如,Perl正则表达式允许在表达式本身中执行代码,许多其他高级运算符和Perl中的一些语法在其他语言中是不同的(即:许多语言使用\>和\ <作为单词边界标记,但perl只使用'\ b')< p>

Spend a few minutes to read some of the PerlRe Documentation and you'll discover lots of awesome tricks that Perl's regular expression engine can do that nothing else seems to do.

花几分钟时间阅读一些PerlRe文档,你会发现Perl的正则表达式引擎可以做的许多令人敬畏的技巧,似乎没有别的办法。

#1


31  

The Java matches method is testing whether the regex matches the entire String. To test whether a regex can be found anywhere in a string, create a Matcher and use its find method.

Java matches方法正在测试正则表达式是否与整个String匹配。要测试是否可以在字符串中的任何位置找到正则表达式,请创建一个Matcher并使用其find方法。

#2


10  

Additionally, the Perl regex syntax is NOT the Java Regex Syntax.

另外,Perl正则表达式语法不是Java Regex语法。

It doesn't apply necessarily in this case, but this is a more answer to your more general question.

在这种情况下,它不一定适用,但这是对更一般性问题的更多回答。

Java has a regular expression syntax known as "PCRE", ie: Perl Compatible.

Java具有称为“PCRE”的正则表达式语法,即:Perl Compatible。

This name is however grossly misleading, because there is very very little about it which is really Perl compatible.

然而,这个名称非常具有误导性,因为它与Perl兼容并非常少。

For instance, Perl regular expressions permit executing code in the expression itself, and lots of other advanced operators, and some syntax are different in Perl as they are in other languages ( ie: many languages use \> and \< as word boundary markers, but Perl just uses '\b' )

例如,Perl正则表达式允许在表达式本身中执行代码,许多其他高级运算符和Perl中的一些语法在其他语言中是不同的(即:许多语言使用\>和\ <作为单词边界标记,但perl只使用'\ b')< p>

Spend a few minutes to read some of the PerlRe Documentation and you'll discover lots of awesome tricks that Perl's regular expression engine can do that nothing else seems to do.

花几分钟时间阅读一些PerlRe文档,你会发现Perl的正则表达式引擎可以做的许多令人敬畏的技巧,似乎没有别的办法。