Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Basics
Anchor start
Anchor end
Repeats
Alternatives
Character classes
POSIX classes
Capture groups
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
C Regex
Basics
Any char (alpha) char* p = ".";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Any char (digit) char* p = ".";
char* s = "2";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Any char (punct) char* p = ".";
char* s = ";";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Any char (\n) // '.' matches anything except newline \n
// but only in REG_NEWLINE mode!
char* p = ".";
char* s = "\n";
regcomp(&r, p, REG_NEWLINE);
t=(regexec(&r, s, n, m, 0)==0);
false
Specific char (alpha) char* p = "a";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Specific char (alpha) char* p = "a";
char* s = "b";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Specific char (oct) char* p = "\141";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Specific char (oct) char* p = "\141";
char* s = "b";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Specific char (hex) char* p = "\x61";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Specific char (hex) char* p = "\x61";
char* s = "b";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Anchor start
Start of string char* p = "^hello";
char* s = "hello world";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Start of string char* p = "^world";
char* s = "hello world";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Start of line char* p = "^world";
char* s = "hello\nworld";
regcomp(&r, p, REG_NEWLINE);
t=(regexec(&r, s, n, m, 0)==0);
true
Start of line char* p = "^world";
char* s = "hello world";
regcomp(&r, p, REG_NEWLINE);
t=(regexec(&r, s, n, m, 0)==0);
false
Start of word char* p = "\\bworld";
char* s = "hello world";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Start of word char* p = "\\bworld";
char* s = "helloworld";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Start of buffer char* p = "\\`hello";
char* s = "hello\nworld";
regcomp(&r, p, REG_NEWLINE);
t=(regexec(&r, s, n, m, 0)==0);
true
Start of buffer char* p = "\\`world";
char* s = "hello\nworld";
regcomp(&r, p, REG_NEWLINE);
t=(regexec(&r, s, n, m, 0)==0);
false
Anchor end
End of string char* p = "world$";
char* s = "hello world";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
End of string char* p = "hello$";
char* s = "hello world";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
End of line char* p = "hello$";
char* s = "hello\nworld";
regcomp(&r, p, REG_NEWLINE);
t=(regexec(&r, s, n, m, 0)==0);
true
End of line char* p = "hello$";
char* s = "hello world";
regcomp(&r, p, REG_NEWLINE);
t=(regexec(&r, s, n, m, 0)==0);
false
End of word char* p = "hello\\b";
char* s = "hello world";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
End of word char* p = "hello\\b";
char* s = "helloworld";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
End of buffer char* p = "world\\'";
char* s = "hello\nworld";
regcomp(&r, p, REG_NEWLINE);
t=(regexec(&r, s, n, m, 0)==0);
true
End of buffer char* p = "hello\\'";
char* s = "hello\nworld";
regcomp(&r, p, REG_NEWLINE);
t=(regexec(&r, s, n, m, 0)==0);
false
Repeats
Zero or more (0) char* p = "a*b";
char* s = "b";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Zero or more (1) char* p = "a*b";
char* s = "ab";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Zero or more (>1) char* p = "a*b";
char* s = "aaaaaab";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
One or more (0) char* p = "a+b";
char* s = "b";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
false
One or more (1) char* p = "a+b";
char* s = "ab";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
One or more (>1) char* p = "a+b";
char* s = "aaaaaab";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
Zero or one (0) char* p = "ca?b";
char* s = "cb";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
Zero or one (1) char* p = "ca?b";
char* s = "cab";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
Zero or one (>1) char* p = "ca?b";
char* s = "caab";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
false
N (N) char* p = "^a{2}$";
char* s = "aa";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
N (>N) char* p = "^a{2}$";
char* s = "aaa";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
false
N (<N) char* p = "^a{2}$";
char* s = "a";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
false
N or more (N) char* p = "^a{2,}$";
char* s = "aa";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
N or more (>N) char* p = "^a{2,}$";
char* s = "aaaaaaa";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
N or more (<N) char* p = "^a{2,}$";
char* s = "a";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
false
N or less (N) char* p = "^a{,2}$";
char* s = "aa";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
N or less (<N) char* p = "^a{,2}$";
char* s = "a";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
N or less (>N) char* p = "^a{,2}$";
char* s = "aaa";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
false
Between N and M (N) char* p = "^a{2,3}$";
char* s = "aa";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
Between N and M (M) char* p = "^a{2,3}$";
char* s = "aaa";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
Between N and M (<N) char* p = "^a{2,3}$";
char* s = "a";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
false
Between N and M (>M) char* p = "^a{2,3}$";
char* s = "aaaa";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
false
Alternatives
String A or B (A) char* p = "abc|def";
char* s = "abc";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
String A or B (B) char* p = "abc|def";
char* s = "def";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
String A or B (neither) char* p = "abc|def";
char* s = "hello";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
false
Grouped A or B (A) char* p = "abc(d|ef)";
char* s = "abcd";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
Grouped A or B (B) char* p = "abc(d|ef)";
char* s = "abcef";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
true
Grouped A or B (neither) char* p = "abc(d|ef)";
char* s = "abcf";
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0)==0);
false
Character classes
Literal (included) char* p = "[abc]";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Literal (included) char* p = "[abc]";
char* s = "b";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Literal (excluded) char* p = "[abc]";
char* s = "d";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Range (inside) char* p = "[a-f]";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Range (inside) char* p = "[a-f]";
char* s = "c";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Range (outside) char* p = "[a-f]";
char* s = "g";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Negated literal (excluded) char* p = "[^abc]";
char* s = "d";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Negated literal (included) char* p = "[^abc]";
char* s = "b";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Negated range (outside) char* p = "[^c-g]";
char* s = "b";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Negated range (outside) char* p = "[^c-g]";
char* s = "h";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Negated range (inside) char* p = "[^c-g]";
char* s = "c";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Negated range (inside) char* p = "[^c-g]";
char* s = "g";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
POSIX character classes
Uppercase letters char* p = "[[:upper:]]";
char* s = "A";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Uppercase letters char* p = "[[:upper:]]";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Uppercase letters char* p = "[[:upper:]]";
char* s = "1";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Uppercase letters char* p = "[[:upper:]]";
char* s = "-";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Lowercase letters char* p = "[[:lower:]]";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Lowercase letters char* p = "[[:lower:]]";
char* s = "A";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Lowercase letters char* p = "[[:lower:]]";
char* s = "1";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Lowercase letters char* p = "[[:lower:]]";
char* s = "-";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
All letters char* p = "[[:alpha:]]";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
All letters char* p = "[[:alpha:]]";
char* s = "A";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
All letters char* p = "[[:alpha:]]";
char* s = "1";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
All letters char* p = "[[:alpha:]]";
char* s = ",";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
All letters and digits char* p = "[[:alnum:]]";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
All letters and digits char* p = "[[:alnum:]]";
char* s = "A";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
All letters and digits char* p = "[[:alnum:]]";
char* s = "1";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
All letters and digits char* p = "[[:alnum:]]";
char* s = ";";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Digits char* p = "[[:digit:]]";
char* s = "0";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Digits char* p = "[[:digit:]]";
char* s = "5";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Digits char* p = "[[:digit:]]";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Digits char* p = "[[:digit:]]";
char* s = "B";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Digits char* p = "[[:digit:]]";
char* s = ".";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Hexadecimal digits char* p = "[[:xdigit:]]";
char* s = "0";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Hexadecimal digits char* p = "[[:xdigit:]]";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Hexadecimal digits char* p = "[[:xdigit:]]";
char* s = "B";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Hexadecimal digits char* p = "[[:xdigit:]]";
char* s = "g";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Punctuation char* p = "^[[:punct:]]*$";
char* s = ".,;:-/@#~%${}()[]+";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Punctuation char* p = "[[:punct:]]";
char* s = "a";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Punctuation char* p = "[[:punct:]]";
char* s = "2";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Punctuation char* p = "[[:punct:]]";
char* s = " ";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Whitespace char* p = "^[[:space:]]*$";
char* s = " \r \n \t \t ";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Whitespace char* p = "^[[:space:]]*$";
char* s = "abc";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Blanks char* p = "^[[:blank:]]*$";
char* s = " \t \t";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
true
Blanks char* p = "^[[:blank:]]*$";
char* s = "\n";
regcomp(&r, p, 0);
t=(regexec(&r, s, n, m, 0)==0);
false
Capture groups
Group 1 char* p = "([0-9]+)([a-z]+)";
char* s = "123abc";
i=1; // group 1
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0) == 0);
if (t)
{
  len = m[i].rm_eo - m[i].rm_so;
  strncpy(g, s + m[i].rm_so, len);
  g[len] = '\0';
}
123
Group 2 char* p = "([0-9]+)([a-z]+)";
char* s = "123abc";
i=2; // group 2
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0) == 0);
if (t)
{
  len = m[i].rm_eo - m[i].rm_so;
  strncpy(g, s + m[i].rm_so, len);
  g[len] = '\0';
}
abc
Optional group 1 - matched char* p = "([0-9]+)*([a-z]+)";
char* s = "123abc";
i=1; // group 1
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0) == 0);
if (t)
{
  len = m[i].rm_eo - m[i].rm_so;
  strncpy(g, s + m[i].rm_so, len);
  g[len] = '\0';
}
123
Optional group 1 - not matched char* p = "([0-9]+)*([a-z]+)";
char* s = "abc";
i=1; // group 1
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0) == 0);
if (t)
{
  len = m[i].rm_eo - m[i].rm_so;
  strncpy(g, s + m[i].rm_so, len);
  g[len] = '\0';
  if (strlen(g)==0)
  {
    strcpy(g,"None");
  }
}
None
Group 2 following optional group not matched char* p = "([0-9]+)*([a-z]+)";
char* s = "abc";
i=2; // group 2
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0) == 0);
if (t)
{
  len = m[i].rm_eo - m[i].rm_so;
  strncpy(g, s + m[i].rm_so, len);
  g[len] = '\0';
}
abc
Complete match char* p = "([0-9]+)*-([a-z]+)";
char* s = "123-abc";
i=0;
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0) == 0);
if (t)
{
  len = m[i].rm_eo - m[i].rm_so;
  strncpy(g, s + m[i].rm_so, len);
  g[len] = '\0';
}
123-abc
Nested group 1 char* p = "(([0-9]+)([a-z]+))([A-Z]+)";
char* s = "123abcDEF";
i=1;
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0) == 0);
if (t)
{
  len = m[i].rm_eo - m[i].rm_so;
  strncpy(g, s + m[i].rm_so, len);
  g[len] = '\0';
}
123abc
Nested group 2 char* p = "(([0-9]+)([a-z]+))([A-Z]+)";
char* s = "123abcDEF";
i=2;
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0) == 0);
if (t)
{
  len = m[i].rm_eo - m[i].rm_so;
  strncpy(g, s + m[i].rm_so, len);
  g[len] = '\0';
}
123
Nested group 3 char* p = "(([0-9]+)([a-z]+))([A-Z]+)";
char* s = "123abcDEF";
i=3;
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0) == 0);
if (t)
{
  len = m[i].rm_eo - m[i].rm_so;
  strncpy(g, s + m[i].rm_so, len);
  g[len] = '\0';
}
abc
Nested group 4 char* p = "(([0-9]+)([a-z]+))([A-Z]+)";
char* s = "123abcDEF";
i=4;
regcomp(&r, p, REG_EXTENDED);
t=(regexec(&r, s, n, m, 0) == 0);
if (t)
{
  len = m[i].rm_eo - m[i].rm_so;
  strncpy(g, s + m[i].rm_so, len);
  g[len] = '\0';
}
DEF