You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oro-user@jakarta.apache.org by "Daniel F. Savarese" <df...@savarese.org> on 2004/02/04 20:20:34 UTC

Re: behaviour of Perl5Matcher.matches doesn't seem to properly take care of alternatives.

In message <40...@blr.cognizant.com>, Tarun Ramakrishna Elankath wri
tes:
>My Perl5Pattern pattern (say ptn) is: \d{5}|\d{9}|\d{12}
...
>I have input string, say zip5, zip9 and zip12 that are strings of digits 
>of length 5, 9 and 12 respectively.
>
>When I use Perl5Matcher.matches(), zip5 passed, but zip9 and zip 12 
>fails. I tried the online demo and it failed there too.
...
>So, I am reduced to specifying my pattern as a cumbersome:
> 				 
>^\d{5}$|^\d{9}$|^\d{12}$
>and using Perl5Matcher.contains. This works fine.

Everything's okay.  You did the right thing.  From the javadocs:

     Note: matches() is not the same as sticking a ^ in front of your
     expression and a $ at the end of your expression in Perl5 and
     using the =~ operator, even though in many cases it will be
     equivalent. matches() literally looks for an exact match
     according to the rules of Perl5 expression matching. Therefore,
     if you have a pattern foo|foot and are matching the input foot
     it will not produce an exact match. But foot|foo will produce an
     exact match for either foot or foo. Remember, Perl5 regular
     expressions do not match the longest possible match. From the
     perlre manpage:


        Alternatives are tried from left to right, so the first
        alternative found for which the entire expression matches, is
        the one that is chosen. This means that alternatives are not
        necessarily greedy. For example: when matching foo|foot
        against "barefoot", only the "foo" part will match, as that is
        the first alternative tried, and it successfully matches the
        target string. 



---------------------------------------------------------------------
To unsubscribe, e-mail: oro-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: oro-user-help@jakarta.apache.org


Re: behaviour of Perl5Matcher.matches doesn't seem to properly take care of alternatives.

Posted by Tarun Ramakrishna Elankath <et...@blr.cognizant.com>.
> Everything's okay.  You did the right thing.  From the javadocs:
I sincerely apologize. I didn't read the docs.. just assumed matches() 
would do what Matcher.matches() of jdk1.5 does.

Though I do believe this is a bit non-intuitive. What would be the 
practical use of having such a method ?


Thanks for making such a great library!
Regards,
Tarun


> 
>      Note: matches() is not the same as sticking a ^ in front of your
>      expression and a $ at the end of your expression in Perl5 and
>      using the =~ operator, even though in many cases it will be
>      equivalent. matches() literally looks for an exact match
>      according to the rules of Perl5 expression matching. Therefore,
>      if you have a pattern foo|foot and are matching the input foot
>      it will not produce an exact match. But foot|foo will produce an
>      exact match for either foot or foo. Remember, Perl5 regular
>      expressions do not match the longest possible match. From the
>      perlre manpage:
> 
> 
>         Alternatives are tried from left to right, so the first
>         alternative found for which the entire expression matches, is
>         the one that is chosen. This means that alternatives are not
>         necessarily greedy. For example: when matching foo|foot
>         against "barefoot", only the "foo" part will match, as that is
>         the first alternative tried, and it successfully matches the
>         target string. 
> 
> 
>