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
"
^ (?P<name> (?:(?P<domain>(?:(?:localhost|[\w-]+(?:\.[\w-]+)+)(?::\d+)?)|[\w]+:\d+)/)? (?P<image>[a-z0-9_.-]+(?:/[a-z0-9_.-]+)*) ) (?::(?P<tag>[\w][\w.-]{0,127}))? (?:@(?P<digest>[A-Za-z][A-Za-z0-9]*(?:[+.-_][A-Za-z][A-Za-z0-9]*)*:[0-9a-fA-F]{32,}))? $
"
gxim
^ asserts position at start of a line
Named Capture Group name
(?P<name> (?:(?P<domain>(?:(?:localhost|[\w-]+(?:\.[\w-]+)+)(?::\d+)?)|[\w]+:\d+)/)? (?P<image>[a-z0-9_.-]+(?:/[a-z0-9_.-]+)*) )
Non-capturing group
(?:(?P<domain>(?:(?:localhost|[\w-]+(?:\.[\w-]+)+)(?::\d+)?)|[\w]+:\d+)/)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Named Capture Group domain
(?P<domain>(?:(?:localhost|[\w-]+(?:\.[\w-]+)+)(?::\d+)?)|[\w]+:\d+)
1st Alternative
(?:(?:localhost|[\w-]+(?:\.[\w-]+)+)(?::\d+)?)
Non-capturing group
(?:(?:localhost|[\w-]+(?:\.[\w-]+)+)(?::\d+)?)
Non-capturing group
(?:localhost|[\w-]+(?:\.[\w-]+)+)
1st Alternative
localhost
localhost
matches the characters localhost literally (case insensitive)
2nd Alternative
[\w-]+(?:\.[\w-]+)+
Match a single character present in the list below
[\w-]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
Non-capturing group
(?:\.[\w-]+)+
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
Match a single character present in the list below
[\w-]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
Non-capturing group
(?::\d+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
: matches the character : with index 5810 (3A16 or 728) literally (case insensitive)
\d
matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Alternative
[\w]+:\d+
Match a single character present in the list below
[\w]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
: matches the character : with index 5810 (3A16 or 728) literally (case insensitive)
\d
matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
Named Capture Group image
(?P<image>[a-z0-9_.-]+(?:/[a-z0-9_.-]+)*)
Match a single character present in the list below
[a-z0-9_.-]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
_.-
matches a single character in the list _.- (case insensitive)
Non-capturing group
(?:/[a-z0-9_.-]+)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
Match a single character present in the list below
[a-z0-9_.-]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
_.-
matches a single character in the list _.- (case insensitive)
Non-capturing group
(?::(?P<tag>[\w][\w.-]{0,127}))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
: matches the character : with index 5810 (3A16 or 728) literally (case insensitive)
Named Capture Group tag
(?P<tag>[\w][\w.-]{0,127})
Match a single character present in the list below
[\w]
\w matches any word character (equivalent to [a-zA-Z0-9_])
Match a single character present in the list below
[\w.-]
{0,127} matches the previous token between 0 and 127 times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
.-
matches a single character in the list .- (case insensitive)
Non-capturing group
(?:@(?P<digest>[A-Za-z][A-Za-z0-9]*(?:[+.-_][A-Za-z][A-Za-z0-9]*)*:[0-9a-fA-F]{32,}))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
@ matches the character @ with index 6410 (4016 or 1008) literally (case insensitive)
Named Capture Group digest
(?P<digest>[A-Za-z][A-Za-z0-9]*(?:[+.-_][A-Za-z][A-Za-z0-9]*)*:[0-9a-fA-F]{32,})
Match a single character present in the list below
[A-Za-z]
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
Match a single character present in the list below
[A-Za-z0-9]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
Non-capturing group
(?:[+.-_][A-Za-z][A-Za-z0-9]*)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[+.-_]
+ matches the character + with index 4310 (2B16 or 538) literally (case insensitive)
.-_ matches a single character in the range between . (index 46) and _ (index 95) (case insensitive)
Match a single character present in the list below
[A-Za-z]
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
Match a single character present in the list below
[A-Za-z0-9]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
: matches the character : with index 5810 (3A16 or 728) literally (case insensitive)
Match a single character present in the list below
[0-9a-fA-F]
{32,} matches the previous token between 32 and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
a-f matches a single character in the range between a (index 97) and f (index 102) (case insensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case insensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
x modifier: extended. Spaces and text after a # in the pattern are ignored
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
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"
"
gxim

Test String