Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Strings
Go
Arrays
Maps
Strings
Java
Arrays
Regex
Basics
Anchor start
Anchor end
Repeats
Alternatives
Character classes
POSIX classes
Pos lookahead
Neg lookahead
Pos lookbehind
Neg lookbehind
Capture groups
Non-greedy
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
Java Regex
Basics
Any char (alpha) String p = ".";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Any char (digit) String p = ".";
String s = "2";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Any char (punct) String p = ".";
String s = ";";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Any char (\n) // '.' matches anything except
// newline \n
String p = ".";
String s = "\n";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Specific char (alpha) String p = "a";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Specific char (alpha) String p = "a";
String s = "b";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Specific char (oct) String p = "\141";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Specific char (oct) String p = "\141";
String s = "b";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Specific char (hex) String p = "\\x61";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Specific char (hex) String p = "\\x61";
String s = "b";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Anchor start
Start of string // "matches" ties to start
// and end of string so no
// need for anchor ^ but do
// need .* prefix
String p = "hello.*";
String s = "hello world";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Start of string [alternative] // "find" searches through
// string so need anchor ^
String p = "^hello";
String s = "hello world";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
true
Start of string String p = "world";
String s = "hello world";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Start of string [alternative] String p = "^world";
String s = "hello world";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
Start of line String p = "^world";
String s = "hello\nworld";
Pattern po = Pattern.compile(p, Pattern.MULTILINE);
Matcher m = po.matcher(s);
t = m.find();
true
Start of line String p = "^world";
String s = "hello world";
Pattern po = Pattern.compile(p, Pattern.MULTILINE);
Matcher m = po.matcher(s);
t = m.find();
false
Start of word String p = "\\bworld";
String s = "hello world";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
true
Start of word String p = "\\bworld";
String s = "helloworld";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
Anchor end
End of string // "matches" ties to start
// and end of string so no
// need for anchor $ but do
// need .* prefix
String p = ".*world";
String s = "hello world";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
End of string [alternative] // "find" searches through
// string so need anchor $
String p = "world$";
String s = "hello world";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
true
End of string String p = "hello";
String s = "hello world";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
End of string [alternative] String p = "hello$";
String s = "hello world";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
End of line String p = "hello$";
String s = "hello\nworld";
Pattern po = Pattern.compile(p, Pattern.MULTILINE);
Matcher m = po.matcher(s);
t = m.find();
true
End of line String p = "hello$";
String s = "hello world";
Pattern po = Pattern.compile(p, Pattern.MULTILINE);
Matcher m = po.matcher(s);
t = m.find();
false
End of word String p = "hello\\b";
String s = "hello world";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
true
End of word String p = "hello\\b";
String s = "helloworld";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
Repeats
Zero or more (0) String p = "a*b";
String s = "b";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Zero or more (1) String p = "a*b";
String s = "ab";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Zero or more (>1) String p = "a*b";
String s = "aaaaaab";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
One or more (0) String p = "a+b";
String s = "b";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
One or more (1) String p = "a+b";
String s = "ab";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
One or more (>1) String p = "a+b";
String s = "aaaaaab";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Zero or one (0) String p = "ca?b";
String s = "cb";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Zero or one (1) String p = "ca?b";
String s = "cab";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Zero or one (>1) String p = "ca?b";
String s = "caab";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
N (N) String p = "a{2}$";
String s = "aa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
N (>N) String p = "a{2}$";
String s = "aaa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
N (<N) String p = "a{2}$";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
N or more (N) String p = "^a{2,}$";
String s = "aa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
N or more (>N) String p = "^a{2,}$";
String s = "aaaaaaa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
N or more (<N) String p = "^a{2,}$";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
N or less (N) String p = "^a{0,2}$";
String s = "aa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
N or less (<N) String p = "^a{0,2}$";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
N or less (>N) String p = "^a{0,2}$";
String s = "aaa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Between N and M (N) String p = "^a{2,3}$";
String s = "aa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Between N and M (M) String p = "^a{2,3}$";
String s = "aaa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Between N and M (<N) String p = "^a{2,3}$";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Between N and M (>M) String p = "^a{2,3}$";
String s = "aaaa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Alternatives
String A or B (A) String p = "abc|def";
String s = "abc";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
String A or B (B) String p = "abc|def";
String s = "def";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
String A or B (neither) String p = "abc|def";
String s = "hello";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Grouped A or B (A) String p = "abc(d|ef)";
String s = "abcd";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Grouped A or B (B) String p = "abc(d|ef)";
String s = "abcef";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Grouped A or B (neither) String p = "abc(d|ef)";
String s = "abcf";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Character classes
Literal (included) String p = "[abc]";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Literal (included) String p = "[abc]";
String s = "b";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Literal (excluded) String p = "[abc]";
String s = "d";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Range (inside) String p = "[a-f]";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Range (inside) String p = "[a-f]";
String s = "c";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Range (outside) String p = "[a-f]";
String s = "g";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Negated literal (excluded) String p = "[^abc]";
String s = "d";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Negated literal (included) String p = "[^abc]";
String s = "b";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Negated range (outside) String p = "[^c-g]";
String s = "b";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Negated range (outside) String p = "[^c-g]";
String s = "h";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Negated range (inside) String p = "[^c-g]";
String s = "c";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Negated range (inside) String p = "[^c-g]";
String s = "g";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
POSIX character classes
Uppercase letters String p = "\\p{Upper}";
String s = "A";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Uppercase letters String p = "\\p{Upper}";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Uppercase letters String p = "\\p{Upper}";
String s = "1";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Uppercase letters String p = "\\p{Upper}";
String s = "-";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Lowercase letters String p = "\\p{Lower}";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Lowercase letters String p = "\\p{Lower}";
String s = "A";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Lowercase letters String p = "\\p{Lower}";
String s = "1";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Lowercase letters String p = "\\p{Lower}";
String s = "-";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
All letters String p = "\\p{Alpha}";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
All letters String p = "\\p{Alpha}";
String s = "A";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
All letters String p = "\\p{Alpha}";
String s = "1";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
All letters String p = "\\p{Alpha}";
String s = ",";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
All letters and digits String p = "\\p{Alnum}";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
All letters and digits String p = "\\p{Alnum}";
String s = "A";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
All letters and digits String p = "\\p{Alnum}";
String s = "1";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
All letters and digits String p = "\\p{Alnum}";
String s = "";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Digits String p = "\\p{Digit}";
String s = "0";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Digits String p = "\\p{Digit}";
String s = "5";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Digits String p = "\\p{Digit}";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Digits String p = "\\p{Digit}";
String s = "B";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Digits String p = "\\p{Digit}";
String s = ".";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Hexadecimal digits String p = "\\p{XDigit}";
String s = "0";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Hexadecimal digits String p = "\\p{XDigit}";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Hexadecimal digits String p = "\\p{XDigit}";
String s = "B";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Hexadecimal digits String p = "\\p{XDigit}";
String s = "g";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Punctuation String p = "^\\p{Punct}*$";
String s = ".,;:-/@#~%${}()[]+";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Punctuation String p = "\\p{Punct}";
String s = "a";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Punctuation String p = "\\p{Punct}";
String s = "2";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Punctuation String p = "\\p{Punct}";
String s = " ";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Whitespace String p = "^\\p{Space}*$";
String s = " \r \n \t \t ";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Whitespace String p = "^\\p{Space}*$";
String s = "abc";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Blanks String p = "^\\p{Blank}*$";
String s = " \t \t";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
true
Blanks String p = "^\\p{Blank}*$";
String s = "\n";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
false
Positive lookahead
A directly followed by B // A=foo, B=bar
String p = "foo(?=bar)";
String s = "foobar";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A directly followed by B // A=foo, B=bar
String p = "foo(?=bar)";
String s = "barfoo";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
A directly followed by B // A=foo, B=bar
String p = "foo(?=bar)";
String s = "foo123bar";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
A followed by B // A=foo, B=bar
String p = "foo(?=.*bar)";
String s = "foobar";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A followed by B // A=foo, B=bar
String p = "foo(?=.*bar)";
String s = "foo123bar";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A followed by B // A=foo, B=bar
String p = "foo(?=.*bar)";
String s = "barfoo";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
Negative lookahead
A not directly followed by B // A=foo, B=bar
String p = "foo(?!bar)";
String s = "foo123";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A not directly followed by B // A=foo, B=bar
String p = "foo(?!bar)";
String s = "foo123bar";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A not directly followed by B // A=foo, B=bar
String p = "foo(?!bar)";
String s = "foobar";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
A not followed by B // A=foo, B=bar
String p = "foo(?!.*bar)";
String s = "foo123";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A not followed by B // A=foo, B=bar
String p = "foo(?!.*bar)";
String s = "foobar";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
A not followed by B // A=foo, B=bar
String p = "foo(?!.*bar)";
String s = "foo123bar";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
Positive lookbehind
A directly preceded by B // A=foo, B=bar
String p = "(?<=bar)foo";
String s = "barfoo";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A directly preceded by B // A=foo, B=bar
String p = "(?<=bar)foo";
String s = "foobar";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
A directly preceded by B // A=foo, B=bar
String p = "(?<=bar)foo";
String s = "bar123foo";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
A preceded by B // A=foo, B=bar
// look-behind requires fixed-width pattern
// therefore "(?<=bar.*)foo" is not allowed
String p = "(?<=bar.{0,8})foo";
String s = "barfoo";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A preceded by B // A=foo, B=bar
// look-behind requires fixed-width pattern
// therefore "(?<=bar.*)foo" is not allowed
String p = "(?<=bar.{0,8})foo";
String s = "bar123foo";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A preceded by B // A=foo, B=bar
// look-behind requires fixed-width pattern
// therefore "(?<=bar.*)foo" is not allowed
String p = "(?<=bar.{0,8})foo";
String s = "foobar";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
Negative lookbehind
A not directly preceded by B // A=foo, B=bar
String p = "(?<!bar)foo";
String s = "123foo";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A not directly preceded by B // A=foo, B=bar
String p = "(?<!bar)foo";
String s = "bar123foo";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A not directly preceded by B // A=foo, B=bar
String p = "(?<!bar)foo";
String s = "barfoo";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
A not preceded by B // A=foo, B=bar
// look-behind requires fixed-width pattern
// therefore "(?<!bar.*)foo" is not allowed
String p = "(?<!bar.{0,8})foo";
String s = "123foo";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A not preceded by B // A=foo, B=bar
// look-behind requires fixed-width pattern
// therefore "(?<!bar.*)foo" is not allowed
String p = "(?<!bar.{0,8})foo";
String s = "foobar";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
String r="false";
if (t)
{
  r=m.group(0);
}
foo
A not preceded by B // A=foo, B=bar
// look-behind requires fixed-width pattern
// therefore "(?<!bar.*)foo" is not allowed
String p = "(?<!bar.{0,8})foo";
String s = "bar123foo";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
false
Capture groups
Group 1 String p = "([0-9]+)([a-z]+)";
String s = "123abc";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
String g = m.group(1);
123
Group 2 String p = "([0-9]+)([a-z]+)";
String s = "123abc";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
String g = m.group(2);
abc
Group 1 following non-capture group String p = "(?:[0-9]+)([a-z]+)";
String s = "123abc";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
String g = m.group(1);
abc
Optional group 1 - matched String p = "([0-9]+)*([a-z]+)";
String s = "123abc";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
String g = m.group(1);
123
Optional group 1 - not matched String p = "([0-9]+)*([a-z]+)";
String s = "abc";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
String g = m.group(1);
if (g==null)
{
  g="None";
}
None
Group 2 following optional group not matched String p = "([0-9]+)*([a-z]+)";
String s = "abc";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
String g = m.group(2);
abc
Complete match String p = "([0-9]+)*-([a-z]+)";
String s = "123-abc";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
String g = m.group(0);
123-abc
Nested group 1 String p = "(([0-9]+)([a-z]+))([A-Z]+)";
String s = "123abcDEF";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
String g = m.group(1);
123abc
Nested group 2 String p = "(([0-9]+)([a-z]+))([A-Z]+)";
String s = "123abcDEF";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
String g = m.group(2);
123
Nested group 3 String p = "(([0-9]+)([a-z]+))([A-Z]+)";
String s = "123abcDEF";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
String g = m.group(3);
abc
Nested group 4 String p = "(([0-9]+)([a-z]+))([A-Z]+)";
String s = "123abcDEF";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.matches();
String g = m.group(4);
DEF
Non-greedy
Greedy zero or more String p = "<.*>";
String s = "<html><head><title> Title </title></html>";
// regex parsing of xml/html
// is not recommended btw!
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
<html><head><title> Title </title></html>
Non-greedy zero or more String p = "<.*?>";
String s = "<html><head><title> Title </title></html>";
// regex parsing of xml/html
// is not recommended btw!
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
<html>
Greedy one or more String p = "a[a-z]+a";
String s = "aaaa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
aaaa
Non-greedy one or more String p = "a[a-z]+?a";
String s = "aaaa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
aaa
Greedy range // picks top of range
String p = "a[a-z]{0,2}a";
String s = "aaaaa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
aaaa
Non-greedy range // picks bottom of range
String p = "a[a-z]{0,2}?a";
String s = "aaaaa";
Pattern po = Pattern.compile(p);
Matcher m = po.matcher(s);
t = m.find();
aa