Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Basics
Anchor start
Anchor end
Repeats
Alternatives
Character classes
POSIX classes
Pos lookahead
Neg lookahead
Capture groups
Non-greedy
Strings
Go
Arrays
Maps
Strings
Java
Arrays
Regex
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
C++ Regex
Basics
Any char (alpha) regex p (".");
string s = "a";
bool t = regex_match(s,p);
true
Any char (digit) regex p (".");
string s = "2";
bool t = regex_match(s,p);
true
Any char (punct) regex p (".");
string s = ";";
bool t = regex_match(s,p);
true
Any char (\n) // '.' matches anything except newline \n
regex p (".");
string s = "\n";
bool t = regex_match(s,p);
false
Specific char (alpha) regex p ("a");
string s = "a";
bool t = regex_match(s,p);
true
Specific char (alpha) regex p ("a");
string s = "b";
bool t = regex_match(s,p);
false
Specific char (oct) regex p ("\141");
string s = "a";
bool t = regex_match(s,p);
true
Specific char (oct) regex p ("\141");
string s = "b";
bool t = regex_match(s,p);
false
Specific char (hex) regex p ("\x61");
string s = "a";
bool t = regex_match(s,p);
true
Specific char (hex) regex p ("\x61");
string s = "b";
bool t = regex_match(s,p);
false
Anchor start
Start of string // "regex_match" tied to start and end
// of string, so trailing .* needed
regex p ("hello.*");
string s = "hello world";
bool t = regex_match(s,p);
true
Start of string [alternative] // "regex_search" searches thru string,
// so anchor ^ needed
regex p ("^hello");
string s = "hello world";
bool t = regex_search(s,p);
true
Start of string regex p ("world.*");
string s = "hello world";
bool t = regex_match(s,p);
false
Start of string [alternative] regex p ("^world");
string s = "hello world";
bool t = regex_search(s,p);
false
Start of word regex p ("\\bworld");
string s = "hello world";
bool t = regex_search(s,p);
true
Start of word regex p ("\\bworld");
string s = "helloworld";
bool t = regex_search(s,p);
false
Anchor end
End of string regex p (".*world");
string s = "hello world";
bool t = regex_match(s,p);
true
End of string [alternative] regex p ("world$");
string s = "hello world";
bool t = regex_search(s,p);
true
End of string regex p ("hello");
string s = "hello world";
bool t = regex_match(s,p);
false
End of string [alternative] regex p ("hello$");
string s = "hello world";
bool t = regex_search(s,p);
false
End of word regex p ("hello\\b");
string s = "hello world";
bool t = regex_search(s,p);
true
End of word regex p ("hello\\b");
string s = "helloworld";
bool t = regex_search(s,p);
false
Repeats
Zero or more (0) regex p ("a*b");
string s = "b";
bool t = regex_match(s,p);
true
Zero or more (1) regex p ("a*b");
string s = "ab";
bool t = regex_match(s,p);
true
Zero or more (>1) regex p ("a*b");
string s = "aaaaaab";
bool t = regex_match(s,p);
true
One or more (0) regex p ("a+b");
string s = "b";
bool t = regex_match(s,p);
false
One or more (1) regex p ("a+b");
string s = "ab";
bool t = regex_match(s,p);
true
One or more (>1) regex p ("a+b");
string s = "aaaaaab";
bool t = regex_match(s,p);
true
Zero or one (0) regex p ("ca?b");
string s = "cb";
bool t = regex_match(s,p);
true
Zero or one (1) regex p ("ca?b");
string s = "cab";
bool t = regex_match(s,p);
true
Zero or one (>1) regex p ("ca?b");
string s = "caab";
bool t = regex_match(s,p);
false
N (N) regex p ("^a{2}$");
string s = "aa";
bool t = regex_match(s,p);
true
N (>N) regex p ("^a{2}$");
string s = "aaa";
bool t = regex_match(s,p);
false
N (<N) regex p ("^a{2}$");
string s = "a";
bool t = regex_match(s,p);
false
N or more (N) regex p ("^a{2,}$");
string s = "aa";
bool t = regex_match(s,p);
true
N or more (>N) regex p ("^a{2,}$");
string s = "aaaaaaa";
bool t = regex_match(s,p);
true
N or more (<N) regex p ("^a{2,}$");
string s = "a";
bool t = regex_match(s,p);
false
N or less (N) regex p ("^a{0,2}$");
string s = "aa";
bool t = regex_match(s,p);
true
N or less (<N) regex p ("^a{0,2}$");
string s = "a";
bool t = regex_match(s,p);
true
N or less (>N) regex p ("^a{0,2}$");
string s = "aaa";
bool t = regex_match(s,p);
false
Between N and M (N) regex p ("^a{2,3}$");
string s = "aa";
bool t = regex_match(s,p);
true
Between N and M (M) regex p ("^a{2,3}$");
string s = "aaa";
bool t = regex_match(s,p);
true
Between N and M (<N) regex p ("^a{2,3}$");
string s = "a";
bool t = regex_match(s,p);
false
Between N and M (>M) regex p ("^a{2,3}$");
string s = "aaaa";
bool t = regex_match(s,p);
false
Alternatives
String A or B (A) regex p ("abc|def");
string s = "abc";
bool t = regex_match(s,p);
true
String A or B (B) regex p ("abc|def");
string s = "def";
bool t = regex_match(s,p);
true
String A or B (neither) regex p ("abc|def");
string s = "hello";
bool t = regex_match(s,p);
false
Grouped A or B (A) regex p ("abc(d|ef)");
string s = "abcd";
bool t = regex_match(s,p);
true
Grouped A or B (B) regex p ("abc(d|ef)");
string s = "abcef";
bool t = regex_match(s,p);
true
Grouped A or B (neither) regex p ("abc(d|ef)");
string s = "abcf";
bool t = regex_match(s,p);
false
Character classes
Literal (included) regex p ("[abc]");
string s = "a";
bool t = regex_match(s,p);
true
Literal (included) regex p ("[abc]");
string s = "b";
bool t = regex_match(s,p);
true
Literal (excluded) regex p ("[abc]");
string s = "d";
bool t = regex_match(s,p);
false
Range (inside) regex p ("[a-f]");
string s = "a";
bool t = regex_match(s,p);
true
Range (inside) regex p ("[a-f]");
string s = "c";
bool t = regex_match(s,p);
true
Range (outside) regex p ("[a-f]");
string s = "g";
bool t = regex_match(s,p);
false
Negated literal (excluded) regex p ("[^abc]");
string s = "d";
bool t = regex_match(s,p);
true
Negated literal (included) regex p ("[^abc]");
string s = "b";
bool t = regex_match(s,p);
false
Negated range (outside) regex p ("[^c-g]");
string s = "b";
bool t = regex_match(s,p);
true
Negated range (outside) regex p ("[^c-g]");
string s = "h";
bool t = regex_match(s,p);
true
Negated range (inside) regex p ("[^c-g]");
string s = "c";
bool t = regex_match(s,p);
false
Negated range (inside) regex p ("[^c-g]");
string s = "g";
bool t = regex_match(s,p);
false
POSIX character classes
Uppercase letters regex p ("[[:upper:]]");
string s = "A";
bool t = regex_match(s,p);
true
Uppercase letters regex p ("[[:upper:]]");
string s = "a";
bool t = regex_match(s,p);
false
Uppercase letters regex p ("[[:upper:]]");
string s = "1";
bool t = regex_match(s,p);
false
Uppercase letters regex p ("[[:upper:]]");
string s = "-";
bool t = regex_match(s,p);
false
Lowercase letters regex p ("[[:lower:]]");
string s = "a";
bool t = regex_match(s,p);
true
Lowercase letters regex p ("[[:lower:]]");
string s = "A";
bool t = regex_match(s,p);
false
Lowercase letters regex p ("[[:lower:]]");
string s = "1";
bool t = regex_match(s,p);
false
Lowercase letters regex p ("[[:lower:]]");
string s = "-";
bool t = regex_match(s,p);
false
All letters regex p ("[[:alpha:]]");
string s = "a";
bool t = regex_match(s,p);
true
All letters regex p ("[[:alpha:]]");
string s = "A";
bool t = regex_match(s,p);
true
All letters regex p ("[[:alpha:]]");
string s = "1";
bool t = regex_match(s,p);
false
All letters regex p ("[[:alpha:]]");
string s = ",";
bool t = regex_match(s,p);
false
All letters and digits regex p ("[[:alnum:]]");
string s = "a";
bool t = regex_match(s,p);
true
All letters and digits regex p ("[[:alnum:]]");
string s = "A";
bool t = regex_match(s,p);
true
All letters and digits regex p ("[[:alnum:]]");
string s = "1";
bool t = regex_match(s,p);
true
All letters and digits regex p ("[[:alnum:]]");
string s = ";";
bool t = regex_match(s,p);
false
Digits regex p ("[[:digit:]]");
string s = "0";
bool t = regex_match(s,p);
true
Digits regex p ("[[:digit:]]");
string s = "5";
bool t = regex_match(s,p);
true
Digits regex p ("[[:digit:]]");
string s = "a";
bool t = regex_match(s,p);
false
Digits regex p ("[[:digit:]]");
string s = "B";
bool t = regex_match(s,p);
false
Digits regex p ("[[:digit:]]");
string s = ".";
bool t = regex_match(s,p);
false
Hexadecimal digits regex p ("[[:xdigit:]]");
string s = "0";
bool t = regex_match(s,p);
true
Hexadecimal digits regex p ("[[:xdigit:]]");
string s = "a";
bool t = regex_match(s,p);
true
Hexadecimal digits regex p ("[[:xdigit:]]");
string s = "B";
bool t = regex_match(s,p);
true
Hexadecimal digits regex p ("[[:xdigit:]]");
string s = "g";
bool t = regex_match(s,p);
false
Punctuation regex p ("^[[:punct:]]*$");
string s = ".,;:-/@#~%${}()[]+";
bool t = regex_match(s,p);
true
Punctuation regex p ("[[:punct:]]");
string s = "a";
bool t = regex_match(s,p);
false
Punctuation regex p ("[[:punct:]]");
string s = "2";
bool t = regex_match(s,p);
false
Punctuation regex p ("[[:punct:]]");
string s = " ";
bool t = regex_match(s,p);
false
Whitespace regex p ("^[[:space:]]*$");
string s = " \r \n \t \t ";
bool t = regex_match(s,p);
true
Whitespace regex p ("^[[:space:]]*$");
string s = "abc";
bool t = regex_match(s,p);
false
Blanks regex p ("^[[:blank:]]*$");
string s = " \t \t";
bool t = regex_match(s,p);
true
Blanks regex p ("^[[:blank:]]*$");
string s = "\n";
bool t = regex_match(s,p);
false
Positive lookahead
A directly followed by B // A=foo, B=bar
regex p ("foo(?=bar)");
string s = "foobar";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
foo
A directly followed by B // A=foo, B=bar
regex p ("foo(?=bar)");
string s = "barfoo";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
false
A directly followed by B // A=foo, B=bar
regex p ("foo(?=bar)");
string s = "foo123bar";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
false
A followed by B // A=foo, B=bar
regex p ("foo(?=.*bar)");
string s = "foobar";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
foo
A followed by B // A=foo, B=bar
regex p ("foo(?=.*bar)");
string s = "foo123bar";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
foo
A followed by B // A=foo, B=bar
regex p ("foo(?=.*bar)");
string s = "barfoo";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
false
Negative lookahead
A not directly followed by B // A=foo, B=bar
regex p ("foo(?!bar)");
string s = "foo123";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
foo
A not directly followed by B // A=foo, B=bar
regex p ("foo(?!bar)");
string s = "foo123bar";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
foo
A not directly followed by B // A=foo, B=bar
regex p ("foo(?!bar)");
string s = "foobar";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
false
A not followed by B // A=foo, B=bar
regex p ("foo(?!.*bar)");
string s = "foo123";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
foo
A not followed by B // A=foo, B=bar
regex p ("foo(?!.*bar)");
string s = "foobar";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
false
A not followed by B // A=foo, B=bar
regex p ("foo(?!.*bar)");
string s = "foo123bar";
smatch m;
bool t = regex_search(s,m,p);
string r="false";
if (t)
{
  r=m[0];
}
false
Capture groups
Group 1 regex p ("([0-9]+)([a-z]+)");
string s = "123abc";
smatch r;
regex_match(s,r,p);
string g=r[1];
123
Group 2 regex p ("([0-9]+)([a-z]+)");
string s = "123abc";
smatch r;
regex_match(s,r,p);
string g=r[2];
abc
Group 1 following non-capture group regex p ("(?:[0-9]+)([a-z]+)");
string s = "123abc";
smatch r;
regex_match(s,r,p);
string g=r[1];
abc
Optional group 1 - matched regex p ("([0-9]+)*([a-z]+)");
string s = "123abc";
smatch r;
regex_match(s,r,p);
string g=r[1];
123
Optional group 1 - not matched regex p ("([0-9]+)*([a-z]+)");
string s = "abc";
smatch r;
bool t = regex_match(s,r,p);
string g="None";
if (t && r[1]!="")
{
  g=r[1];
}
None
Group 2 following optional group not matched regex p ("([0-9]+)*([a-z]+)");
string s = "abc";
smatch r;
regex_match(s,r,p);
string g=r[2];
abc
Complete match regex p ("([0-9]+)*-([a-z]+)");
string s = "123-abc";
smatch r;
regex_match(s,r,p);
string g=r[0];
123-abc
Nested group 1 regex p ("(([0-9]+)([a-z]+))([A-Z]+)");
string s = "123abcDEF";
smatch r;
regex_match(s,r,p);
string g=r[1];
123abc
Nested group 2 regex p ("(([0-9]+)([a-z]+))([A-Z]+)");
string s = "123abcDEF";
smatch r;
regex_match(s,r,p);
string g=r[2];
123
Nested group 3 regex p ("(([0-9]+)([a-z]+))([A-Z]+)");
string s = "123abcDEF";
smatch r;
regex_match(s,r,p);
string g=r[3];
abc
Nested group 4 regex p ("(([0-9]+)([a-z]+))([A-Z]+)");
string s = "123abcDEF";
smatch r;
regex_match(s,r,p);
string g=r[4];
DEF
Non-greedy
Greedy zero or more // regex parsing of xml/html
// is not recommended btw!
regex p ("<.*>");
string s = "<html><head><title> Title </title></html>";
smatch r;
regex_match(s,r,p);
string m=r[0];
<html><head><title> Title </title></html>
Non-greedy zero or more // regex parsing of xml/html
// is not recommended btw!
regex p ("<.*?>");
string s = "<html><head><title> Title </title></html>";
// "regex_search" is required for non-greedy
smatch r;
regex_search(s,r,p);
string m=r[0];
<html>
Greedy one or more regex p ("a[a-z]+a");
string s = "aaaa";
smatch r;
regex_match(s,r,p);
string m=r[0];
aaaa
Non-greedy one or more regex p ("a[a-z]+?a");
string s = "aaaa";
// "regex_search" is required for non-greedy
smatch r;
regex_search(s,r,p);
string m=r[0];
aaa
Greedy range // picks top of range
regex p ("a[a-z]{0,2}a");
string s = "aaaaa";
// "regex_search" is required to match here
smatch r;
regex_search(s,r,p);
string m=r[0];
aaaa
Non-greedy range // picks bottom of range
regex p ("a[a-z]{0,2}?a");
string s = "aaaaa";
// "regex_search" is required for non-greedy
smatch r;
regex_search(s,r,p);
string m=r[0];
aa