Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8

Function

  • Match
  • Substitution
  • List
  • Unit Tests
"
(?i)^[a-f\d]{2}([.:-]?)([a-f\d]{2}\1){4}[a-f\d]{2}$
"
gm
(?i)
match the remainder of the pattern with the following effective flags: gmi
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
^ asserts position at start of a line
Match a single character present in the list below
[a-f\d]
{2} matches the previous token exactly 2 times
a-f matches a single character in the range between a (index 97) and f (index 102) (case insensitive)
\d matches a digit (equivalent to [0-9])
1st Capturing Group
([.:-]?)
Match a single character present in the list below
[.:-]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
.:-
matches a single character in the list .:- (case insensitive)
2nd Capturing Group
([a-f\d]{2}\1){4}
{4} matches the previous token exactly 4 times
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
Match a single character present in the list below
[a-f\d]
{2} matches the previous token exactly 2 times
a-f matches a single character in the range between a (index 97) and f (index 102) (case insensitive)
\d matches a digit (equivalent to [0-9])
\1 matches the same text as most recently matched by the 1st capturing group
Match a single character present in the list below
[a-f\d]
{2} matches the previous token exactly 2 times
a-f matches a single character in the range between a (index 97) and f (index 102) (case insensitive)
\d matches a digit (equivalent to [0-9])
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
gm

Test String