Why do regular expressions in Java and Perl act differently?

前端 未结 2 379
我寻月下人不归
我寻月下人不归 2020-12-10 03:52

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 str

相关标签:
2条回答
  • 2020-12-10 03:53

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

    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.

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

    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' )

    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.

    0 讨论(0)
  • 2020-12-10 04:03

    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.

    0 讨论(0)
提交回复
热议问题