I wrote a bunch of Perl code before Java finally got decent regular expressions. (I wrote lots of C++ before that but C++ didn’t have built in regular expressions either).
For some reason, on a number of occasions my Java regular expressions never worked right and I never fully realized why.
However, when I saw his example, I finally understood my confusion. In Perl, a rage means – “Does this pattern exist somewhere in my target string”?
So the following Perl code:
will print “match”
You can force a regex in Perl to mean match from the beginning of the line by putting line markers into your string.
So the code:
won’t print out “match”
However, in Java, regex’s have to match against the whole string and you need .* on both ends if you want the Perl behavior. So the code:
Will print out: