## Regular Expressions

(see www.javaregex.com for a complete tutorial)
Basic Patterns

- []
  Matches one of the enclosed characters:
  '[xy]z' matches 'xz' and 'yz'
  '[0123456789]z' matches '0z', '1z', ... '9z'
  '[0123456789][0123456789]' matches a 2-char digit.
  Ranges:
  '[A-Z]' matches any upper case char
  '[a-z]' matches any lower case char
  '[A-z]' matches any char
  Negation:
  '[^abc]' matches any char except a,b or c.
  Escape:
  '\[a' matches the string '[a'
- \w
  Shorthand for '[0-9A-Za-z_]' (any char or digit)
- \s
  Shorthand for '[ \b\t\n\r]' (== any Whitespace Character)
- \d
  Shorthand for '[0-9]' (== any digit)
- \W,\S,\D
  The negation of the lower case shorthands.
- \b
  matches word boundary.
  '\bclass\b' matches all occurences of the whole word 'class' but not 'aclass' or 'classes'.
- \B
  does NOT match a word boundary.
  '\bclass\B' matches words starting with 'class' but not the string 'class' as a single word. '\bclass' matches words starting with 'class' and the string 'class' as whole word.
- \A
  matches beginning of a String.
- \Z
  matches end of a String.
- .
  matches anything but a line feed. (equals '[^\n]' )
- ()
  Goup a pattern (see 'Repetition' below)

Pattern Repetition

- *
  matches {0,infite} occurences of preceding pattern.
  'a*' matches any sequence of 'a'. ('aa','aaaaaa')
  '(test)*' matches any sequence of 'test' ('test','testtest', 'testtesttest', .. ), the braces group the pattern
  'test*' would match 'test', 'testt', 'testttttttttt', ..
  '\s*' matches any sequence of whitespace.
- +
  same as '*' but requires at least ONE occurence of the preceding pattern.
- {min,max}
  same as '*' but requires at least 'min' occurences of the preceding pattern, matches not more than 'max' occurences of the preceding pattern.
  '(test){2,3}' matches 'testtest' and 'testtesttest', but not 'testtesttesttest' or 'test'.
- ?
  if a repetition statement is followed by a '?', matching is less hungry. The minimal match is found.
  'a+?' would match the first 'a' in 'aaaaa'. 'a+' would match the full sequence 'aaaaaaaa'.

Matching balanced paranthesis

'(?@())' matches balanced parenthesis,
'(?@[])' matches balanced square brackets,
'(?@{})' matches balanced curly brackets

Pattern | Class Name | Explanation
--- | --- | ---
[a-c] | Range | This matches any character in the range 'a' to 'c'
[a-cde] | Bracket | This matches any character in the range 'a' to 'e'
[\-x] | Bracket | This matches the '-' character or the 'x' character
[^ab] | Bracket | This matches any characters except 'a' or 'b'
. | Any | This matches any character except \n. To match any character at all, you might try [\d\D].
{n1,n2} | Multi | This matches between n1 and n2 instances of the previous Pattern, where n1 and n2 are integers.
{n1,} | Multi | This matches at least n1 instances of the previous Pattern
* | Multi | The same as {0,}
? | Multi | The same as {0,1}
+ | Multi | The same as {1,}
*? | MultiMin | By default, {n1,n2}, {n1,}, *, ?, +, match the largest number of occurences of the preceeding Pattern. If any of these is followed by a ? it will attempt, instead, to match the fewest occurents of the preceeding Pattern.
(a|b) | OrMark | This matches the character a or b, and returns the matched character as a backreference. In other words, if you call Regex's search method with "a" you will find that an "a" is returned by stringMatched(1).
(a) | OrMark | This matches the character "a" as a backreference.
\b | Boundary | This matches a word boundary, either a beginning \w character, an ending \w character, or one of these two sequences: \w\W, \W\w.
(?: ... ) | Or | like the parenthesis above, but does not create a backreference.
(?= ... ) | lookAhead | A zero-length lookahead. Thus the pattern "foo(?=bar)" will match "foo", but only if followed by "bar".
(?! ... ) | lookAhead | Another form of zero-length lookahead. However, it only matches if the thing in the ()'s is not matched. Thus "foo(?!bar)" matches "foo" only if it is not followed by "bar".
(?# ... ) |  | A comment
\B | Boundary | A non-word Boundary. Essentially the same as (?!\b).
\d | Range | Essentially the same as [0-9].
\D | Bracket | Not a digit. Essentially the same as [^0-9].
\w | Bracket | A word character, essentially the same as [a-zA-Z_0-9]
\W | Bracket | Not a word character.
\s | Bracket | A white-space character, [ \t\b\n\r].
\S | Bracket | A non white-space character.
\1 | BackMatch | Match the contents of the first backreference. Thus "([a-d]).*\1" matches the first 5 characters of "axyzabc".
(?i) |  | Tell this pattern to ignore case during a match.
$ | End | Matches the end of a String (the \n character is considered the end of the string by this pattern element).
\Z | End | Matches the end of a String. The \n character does not count as the end.
^ | Start | Matches the beginning of a String. This is either the absolute beginning, or right after a \n character.
\A | Start | Matches the absolute beginning of a String.
\G | BackG | Matches the place we left off in our last search of this String with this pattern or, failing that, the beginning of the String.