Programming
Bash
Arrays
Regex - grep
Basics
Anchor start
Anchor end
Repeats
Alternatives
Character classes
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Strings
Go
Arrays
Maps
Strings
Java
Arrays
Regex
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
Bash Regex
Basics
Any char (alpha) p=".";
s="a";
t=`echo ${s} | grep ${p}`
true
Any char (digit) p=".";
s="2";
t=`echo ${s} | grep ${p}`
true
Any char (punct) p=".";
s=";";
t=`echo ${s} | grep ${p}`
true
Any char (\n) # '.' matches anything except newline \n
p=".";
s="\n";
# -e enables interpretation of \n
t=`echo -e ${s} | grep ${p}`
false
Specific char (alpha) p="a";
s="a";
t=`echo ${s} | grep ${p}`
true
Specific char (alpha) p="a";
s="b";
t=`echo ${s} | grep ${p}`
false
Specific char (oct) p="\141";
s="a";
# -P = using Perl regex syntax for Octal
t=`echo ${s} | grep -P ${p}`
true
Specific char (oct) p="\141";
s="b";
t=`echo ${s} | grep -P ${p}`
false
Specific char (hex) p="\x61";
s="a";
# -P = using Perl regex syntax for Hex
t=`echo ${s} | grep -P ${p}`
true
Specific char (hex) p="\\x61";
s="b";
t=`echo ${s} | grep -P ${p}`
false
Anchor start
Start of string p="^hello";
s="hello world";
t=`echo ${s} | grep ${p}`
true
Start of string p="^world";
s="hello world";
t=`echo ${s} | grep ${p}`
false
Start of line # grep is line-based, so ^ = start of line
p="^world";
s="hello\nworld";
# -e enables interpretation of \n
t=`echo -e ${s} | grep ${p}`
true
Start of line p="^world";
s="hello world";
t=`echo -e ${s} | grep ${p}`
false
Start of word p="\bworld";
s="hello world";
t=`echo ${s} | grep ${p}`
true
Start of word p="\bworld";
s="helloworld";
t=`echo ${s} | grep ${p}`
false
Anchor end
End of string p="world$";
s="hello world";
t=`echo ${s} | grep ${p}`
true
End of string p="hello$";
s="hello world";
t=`echo ${s} | grep ${p}`
false
End of line # grep is line-based, so $ = end of line
p="hello$";
s="hello\nworld";
# -e enables interpretation of \n
t=`echo -e ${s} | grep ${p}`
true
End of line p="hello$";
s="hello world";
t=`echo -e ${s} | grep ${p}`
false
End of word p="hello\b";
s="hello world";
t=`echo ${s} | grep ${p}`
true
End of word p="hello\b";
s="helloworld";
t=`echo ${s} | grep ${p}`
false
Repeats
Zero or more (0) p="a*b";
s="b";
t=`echo ${s} | grep ${p}`
true
Zero or more (1) p="a*b";
s="ab";
t=`echo ${s} | grep ${p}`
true
Zero or more (>1) p="a*b";
s="aaaaaab";
t=`echo ${s} | grep ${p}`
true
One or more (0) p="a+b";
s="b";
# egrep required to use + in regex
t=`echo ${s} | egrep ${p}`
false
One or more (1) p="a+b";
s="ab";
t=`echo ${s} | egrep ${p}`
true
One or more (>1) p="a+b";
s="aaaaaab";
t=`echo ${s} | egrep ${p}`
true
Zero or one (0) p="ca?b";
s="cb";
# egrep required to use ? in regex
t=`echo ${s} | egrep ${p}`
true
Zero or one (1) p="ca?b";
s="cab";
t=`echo ${s} | egrep ${p}`
true
Zero or one (>1) p="ca?b";
s="caab";
t=`echo ${s} | grep ${p}`
false
N (N) p="^a{2}$";
s="aa";
# egrep required to use {N} in regex
t=`echo ${s} | egrep ${p}`
true
N (>N) p="^a{2}$";
s="aaa";
t=`echo ${s} | egrep ${p}`
false
N (<N) p="^a{2}$";
s="a";
t=`echo ${s} | egrep ${p}`
false
N or more (N) p="^a{2,}$";
s="aa";
# egrep required to use {N,} in regex
t=`echo ${s} | egrep ${p}`
true
N or more (>N) p="^a{2,}$";
s="aaaaaaa";
t=`echo ${s} | egrep ${p}`
true
N or more (<N) p="^a{2,}$";
s="a";
t=`echo ${s} | egrep ${p}`
false
N or less (N) p="^a{0,2}$";
s="aa";
# egrep required to use {0,N} in regex
t=`echo ${s} | egrep ${p}`
true
N or less (<N) p="^a{0,2}$";
s="a";
t=`echo ${s} | egrep ${p}`
true
N or less (>N) p="^a{0,2}$";
s="aaa";
t=`echo ${s} | egrep ${p}`
false
Between N and M (N) p="^a{2,3}$";
s="aa";
# egrep required to use {N,M} in regex
t=`echo ${s} | egrep ${p}`
true
Between N and M (M) p="^a{2,3}$";
s="aaa";
t=`echo ${s} | egrep ${p}`
true
Between N and M (<N) p="^a{2,3}$";
s="a";
t=`echo ${s} | egrep ${p}`
false
Between N and M (>M) p="^a{2,3}$";
s="aaaa";
t=`echo ${s} | egrep ${p}`
false
Alternatives
String A or B (A) p="abc|def";
s="abc";
# egrep required to use | in regex
t=`echo ${s} | egrep ${p}`
true
String A or B (B) p="abc|def";
s="def";
t=`echo ${s} | egrep ${p}`
true
String A or B (neither) p="abc|def";
s="hello";
t=`echo ${s} | egrep ${p}`
false
Grouped A or B (A) p="abc(d|ef)";
s="abcd";
t=`echo ${s} | egrep ${p}`
true
Grouped A or B (B) p="abc(d|ef)";
s="abcef";
t=`echo ${s} | egrep ${p}`
true
Grouped A or B (neither) p="abc(d|ef)";
s="abcf";
t=`echo ${s} | egrep ${p}`
false
Character classes
Literal (included) p="[abc]";
s="a";
t=`echo ${s} | grep ${p}`
true
Literal (included) p="[abc]";
s="b";
t=`echo ${s} | grep ${p}`
true
Literal (excluded) p="[abc]";
s="d";
t=`echo ${s} | grep ${p}`
false
Range (inside) p="[a-f]";
s="a";
t=`echo ${s} | grep ${p}`
true
Range (inside) p="[a-f]";
s="c";
t=`echo ${s} | grep ${p}`
true
Range (outside) p="[a-f]";
s="g";
t=`echo ${s} | grep ${p}`
false
Negated literal (excluded) p="[^abc]";
s="d";
t=`echo ${s} | grep ${p}`
true
Negated literal (included) p="[^abc]";
s="b";
t=`echo ${s} | grep ${p}`
false
Negated range (outside) p="[^c-g]";
s="b";
t=`echo ${s} | grep ${p}`
true
Negated range (inside) p="[^c-g]";
s="c";
t=`echo ${s} | grep ${p}`
false
Negated range (inside) p="[^c-g]";
s="g";
t=`echo ${s} | grep ${p}`
false