Regular expressions |
| BASIC |
A regular expression is a String that describes a pattern; the pattern may be applied to another string, to determine if it matches the pattern, or to find and/or replace parts of that other string.
While regular expressions are an advanced topic, the beginner should be aware of two important facts about them:
boolean matches(String regex)- Tests whether this string matches the
regexpattern.
String replaceFirst(String regex, String replacement)- Replaces the first substring that matches the
regexpattern with thereplacementString.
String replaceAll(String regex, String replacement)- Replaces every substring that matches the
regexpattern with thereplacementString.
public String[] split(String regex)- Strings matched by the
regexpattern act as separators; the substrings thus separated are returned in an array.
public String[] split(String regex, int limit)- Returns an array of not more than
limitsubstrings.
| ADVANCED |
java.util.regex.Pattern. | STYLE |
Regular expressions are very powerful, and should be a part of every programmer's toolbox.
However, regular expressions that use characters with special meanings can be extraordinarily inefficient. Any use of such regular expressions should be tested to determine not only their results, but also the sequence of operations performed in order to return those results. Often, very minor changes in a regular expression can have major consequences for efficiency.