Java Reference
In-Depth Information
How It Works
The String methods that work with regular expressions are the following:
public boolean matches(String regex)
public String replaceFirst(String regex, String
replacement)
public String replaceAll(String regex, String re-
placement)
public String[] split(String regex, int limit)
public String[] split(String regex)
The String methods are limited and relatively simple wrappers around the more
powerful functionality of the java.util.regex classes:
java.util.regex.Pattern
java.util.regex.Matcher
java.util.regex.PatternSyntaxException
The Java regular expressions are similar to those used in the Perl language. Al-
though there is a lot to learn about Java regular expressions, probably the most import-
ant points to understand from this recipe are these:
Your regular expressions can definitely contain non-ASCII characters
from the full range of Unicode characters.
Because of a peculiarity of how the Java language compiler understands
the backslash character, you will have to use two backslashes in your
code instead of one for the predefined character class expressions.
The most convenient and readable way to use non-ASCII characters in regular ex-
pressions is to type them directly into your source files using your keyboard input
methods. Operating systems and editors differ in how they allow you to enter complex
text outside of ASCII. Regardless of operating system, you should save the file in the
UTF-8 encoding if your editor allows. As an alternate but more difficult way to use
non-ASCII regular expressions, you can encode characters using the \uXXXX notation.
Search WWH ::




Custom Search