Java Reference
In-Depth Information
Table2-2.Regular expression metacharacters
{} Indicates the number of times to match the preceding pattern, e.g., \d{3} matches any three digits in a
row.
[] Matches any pattern inside the brackets once and only once, e.g., [AB] matches either a single A or B
character (case-sensitive) and nothing else.
- When used inside [] , defines a range separator, e.g, [1-5] matches any single value 1 through 5. More
than one range can be used inside a list, e.g., [1-5A-E] matches any single value 1 through 5 OR a single
case-sensitive character A,B,C,D, or E.
^ When used inside [] , negates the expression, e.g., [^0] matches anything but 0.
Spaces inside brackets are meaningful. Parentheses are used for grouping, and must be es-
caped if intended literally. Here are some more brief examples:
capitali[zs]e matches the American “capitalize” or the British “capitalise”.
honou?r matches the American “honor” or the British “honour”.
\(?\d{3}\)?[ \-\.]?\d{3}[ \-\.]?\d{4} matches 888.999.1111 or (888)999-1111
or (888) 999.1111 or (888)-999.1111. Let's dissect part of this phone construct. Take just
this part: \(?\d{3}\)? . The first slash means escape the opening parenthesis to indicate
that you mean to match the parentheses character, not a group. The first ? means that the
opening parenthesis character preceding it is optional: there can be 0 or 1. The \d{3}
matches three digits. You then match the mirror of the opening construct \)? to indicate
an optional closing parenthesis. Now let's look at the next section: [ \-\.]? . The square
brackets indicate a range. The space character is meaningful. This snippet indicates that
following the area code can be a single space, a hyphen, a dot ( . ), or none of these. The
remainder of the phone number regex repeats variations on these patterns.
If you know the structure of a data type, it is probably a good idea to constrain the values
with regular expressions. But to ensure the best chance of interoperability, it is probably best
to keep your regex constructs fairly simple, and not to use optional extensions that are not
available on every platform. These include positioning constructs, such as \< to match the be-
ginning of a word, and so on.
See Also
Generating Java Classes from Schema .
Search WWH ::




Custom Search