Regular Expressions

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

Basic Patterns Pattern Repetition Matching balanced paranthesis

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



PatternClass NameExplanation
[a-c]RangeThis matches any character in the range 'a' to 'c'
[a-cde]BracketThis matches any character in the range 'a' to 'e'
[\-x]BracketThis matches the '-' character or the 'x' character
[^ab]BracketThis matches any characters except 'a' or 'b'
.AnyThis matches any character except \n. To match any character at all, you might try [\d\D].
{n1,n2}MultiThis matches between n1 and n2 instances of the previous Pattern, where n1 and n2 are integers.
{n1,}MultiThis matches at least n1 instances of the previous Pattern
*MultiThe same as {0,}
?MultiThe same as {0,1}
+MultiThe same as {1,}
*?MultiMinBy 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)OrMarkThis 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)OrMarkThis matches the character "a" as a backreference.
\bBoundaryThis matches a word boundary, either a beginning \w character, an ending \w character, or one of these two sequences: \w\W, \W\w.
(?: ... )Orlike the parenthesis above, but does not create a backreference.
(?= ... )lookAheadA zero-length lookahead. Thus the pattern "foo(?=bar)" will match "foo", but only if followed by "bar".
(?! ... )lookAheadAnother 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
\BBoundaryA non-word Boundary. Essentially the same as (?!\b).
\dRangeEssentially the same as [0-9].
\DBracketNot a digit. Essentially the same as [^0-9].
\wBracketA word character, essentially the same as [a-zA-Z_0-9]
\WBracketNot a word character.
\sBracketA white-space character, [ \t\b\n\r].
\SBracketA non white-space character.
\1BackMatchMatch 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.
$EndMatches the end of a String (the \n character is considered the end of the string by this pattern element).
\ZEndMatches the end of a String. The \n character does not count as the end.
^StartMatches the beginning of a String. This is either the absolute beginning, or right after a \n character.
\AStartMatches the absolute beginning of a String.
\GBackGMatches the place we left off in our last search of this String with this pattern or, failing that, the beginning of the String.