I am primarily a C programmer, with an additional love for Perl. One thing that attracts me to Java is that it is a less esoteric language than Perl, but has a much richer standard library, with much of the functionality that I love about Perl.
This page is not a Java tutorial. It is a bunch of tips and techniques that I have learned the hard way, by crashing into a brick wall and groping for the door. Maybe these tips will save you from a bloody nose.
Regular expressions
- match - the "match()" method is intended to match the entire string. I.e. it implies "^" and "$" at the start and end of the pattern. If you want to a Perl-style non-anchored patterns, use the "find" method, which unfortunately is not available in the String class. You need to create a Pattern and a Matcher. Also, the "find" method is a stateful method - you can call it multiple times to match subsequent substrings that match.
- String equality - Gotta love OO languages, with their overloading and such. In Perl, if you have two String objects and you want to see if they contain the same string, you might try "if (x == y)". And you would be wrong. That checks to see if the two objects are in fact the same object. To test for string equality, you must use the "equals()" method of one of the strings: "if (x.equals(y))".
- Backslash - In Perl, you use backslash (\) to escape meta-characters. If Java source code, you specify patterns as Java strings. Therefore, to pass a backslash to Java's RE engine, you need to use a double-backslash in your source code.
Reference
-