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
"
(?:[\b\B]?(?:(?P<EngDate>[a-zA-Z]+ )?(?P<Date0>(?:\d{1,2}(?=[ ,./-])|\d{4})?)\.??(?(2),?)(?P<d_sep>[ ,./-])?(?:(?P<Date1>[^\W\d_]+|\d{1,2})(?(3)(?P=d_sep)))?(?P<Date2>\d{4}|\d{2})?(?!\:|\.|\d))|(?:(?<!\d)(?P<Hours>[0-2]?\d)[:\.](?P<Minutes>[0-5]\d)(?:[:\.](?P<Seconds>[0-5]\d))?(?:[,\.](?P<SenodsFraction>\d{6}|\d{3}))?(?: \w\W)?(?:(?:[\. \w+]?(?:(?P<Z>(?:[a-zA-Z]{3,5})|(?:[+-]\d\d?:?[0,3,4]?[0,5]?))|(?P<s>PD|[apmAPM]{2})))+)?))
"
g
Non-capturing group
(?:[\b\B]?(?:(?P<EngDate>[a-zA-Z]+ )?(?P<Date0>(?:\d{1,2}(?=[ ,./-])|\d{4})?)\.??(?(2),?)(?P<d_sep>[ ,./-])?(?:(?P<Date1>[^\W\d_]+|\d{1,2})(?(3)(?P=d_sep)))?(?P<Date2>\d{4}|\d{2})?(?!\:|\.|\d))|(?:(?<!\d)(?P<Hours>[0-2]?\d)[:\.](?P<Minutes>[0-5]\d)(?:[:\.](?P<Seconds>[0-5]\d))?(?:[,\.](?P<SenodsFraction>\d{6}|\d{3}))?(?: \w\W)?(?:(?:[\. \w+]?(?:(?P<Z>(?:[a-zA-Z]{3,5})|(?:[+-]\d\d?:?[0,3,4]?[0,5]?))|(?P<s>PD|[apmAPM]{2})))+)?))
1st Alternative
[\b\B]?(?:(?P<EngDate>[a-zA-Z]+ )?(?P<Date0>(?:\d{1,2}(?=[ ,./-])|\d{4})?)\.??(?(2),?)(?P<d_sep>[ ,./-])?(?:(?P<Date1>[^\W\d_]+|\d{1,2})(?(3)(?P=d_sep)))?(?P<Date2>\d{4}|\d{2})?(?!\:|\.|\d))
Match a single character present in the list below
[\b\B]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\b matches a backspace character (ASCII 8)
\B matches the character B with index 6610 (4216 or 1028) literally (case sensitive)
Non-capturing group
(?:(?P<EngDate>[a-zA-Z]+ )?(?P<Date0>(?:\d{1,2}(?=[ ,./-])|\d{4})?)\.??(?(2),?)(?P<d_sep>[ ,./-])?(?:(?P<Date1>[^\W\d_]+|\d{1,2})(?(3)(?P=d_sep)))?(?P<Date2>\d{4}|\d{2})?(?!\:|\.|\d))
Named Capture Group EngDate
(?P<EngDate>[a-zA-Z]+ )?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[a-zA-Z]
+ 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 sensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Named Capture Group Date0
(?P<Date0>(?:\d{1,2}(?=[ ,./-])|\d{4})?)
Non-capturing group
(?:\d{1,2}(?=[ ,./-])|\d{4})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative
\d{1,2}(?=[ ,./-])
\d
matches a digit (equivalent to [0-9])
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
Positive Lookahead
(?=[ ,./-])
Assert that the Regex below matches
Match a single character present in the list below
[ ,./-]
,./-
matches a single character in the list ,./- (case sensitive)
2nd Alternative
\d{4}
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
\.
matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
?? matches the previous token between zero and one times, as few times as possible, expanding as needed (lazy)
Conditional
(?(2),?)
Conditionally matches one of two options depending on whether the 2nd capturing group matched
If condition is met, match the following regex
,?
,
matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
Named Capture Group d_sep
(?P<d_sep>[ ,./-])?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[ ,./-]
,./-
matches a single character in the list ,./- (case sensitive)
Non-capturing group
(?:(?P<Date1>[^\W\d_]+|\d{1,2})(?(3)(?P=d_sep)))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Named Capture Group Date1
(?P<Date1>[^\W\d_]+|\d{1,2})
1st Alternative
[^\W\d_]+
Match a single character not present in the list below
[^\W\d_]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\W matches any non-word character (equivalent to [^a-zA-Z0-9_])
\d matches a digit (equivalent to [0-9])
_ matches the character _ with index 9510 (5F16 or 1378) literally (case sensitive)
2nd Alternative
\d{1,2}
\d
matches a digit (equivalent to [0-9])
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
Conditional
(?(3)(?P=d_sep))
Conditionally matches one of two options depending on whether the 3rd capturing group matched
If condition is met, match the following regex
(?P=d_sep)
(?P=d_sep) matches the same text as most recently matched by the capturing group named d_sep
Named Capture Group Date2
(?P<Date2>\d{4}|\d{2})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative
\d{4}
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
2nd Alternative
\d{2}
\d
matches a digit (equivalent to [0-9])
{2} matches the previous token exactly 2 times
Negative Lookahead
(?!\:|\.|\d)
Assert that the Regex below does not match
1st Alternative
\:
\: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
2nd Alternative
\.
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
3rd Alternative
\d
\d matches a digit (equivalent to [0-9])
2nd Alternative
(?:(?<!\d)(?P<Hours>[0-2]?\d)[:\.](?P<Minutes>[0-5]\d)(?:[:\.](?P<Seconds>[0-5]\d))?(?:[,\.](?P<SenodsFraction>\d{6}|\d{3}))?(?: \w\W)?(?:(?:[\. \w+]?(?:(?P<Z>(?:[a-zA-Z]{3,5})|(?:[+-]\d\d?:?[0,3,4]?[0,5]?))|(?P<s>PD|[apmAPM]{2})))+)?)
Non-capturing group
(?:(?<!\d)(?P<Hours>[0-2]?\d)[:\.](?P<Minutes>[0-5]\d)(?:[:\.](?P<Seconds>[0-5]\d))?(?:[,\.](?P<SenodsFraction>\d{6}|\d{3}))?(?: \w\W)?(?:(?:[\. \w+]?(?:(?P<Z>(?:[a-zA-Z]{3,5})|(?:[+-]\d\d?:?[0,3,4]?[0,5]?))|(?P<s>PD|[apmAPM]{2})))+)?)
Negative Lookbehind
(?<!\d)
Assert that the Regex below does not match
\d matches a digit (equivalent to [0-9])
Named Capture Group Hours
(?P<Hours>[0-2]?\d)
Match a single character present in the list below
[0-2]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
0-2 matches a single character in the range between 0 (index 48) and 2 (index 50) (case sensitive)
\d matches a digit (equivalent to [0-9])
Match a single character present in the list below
[:\.]
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
Named Capture Group Minutes
(?P<Minutes>[0-5]\d)
Match a single character present in the list below
[0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (case sensitive)
\d matches a digit (equivalent to [0-9])
Non-capturing group
(?:[:\.](?P<Seconds>[0-5]\d))?
? matches the previous token between zero and one 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 5810 (3A16 or 728) literally (case sensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
Named Capture Group Seconds
(?P<Seconds>[0-5]\d)
Match a single character present in the list below
[0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (case sensitive)
\d matches a digit (equivalent to [0-9])
Non-capturing group
(?:[,\.](?P<SenodsFraction>\d{6}|\d{3}))?
? matches the previous token between zero and one 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 4410 (2C16 or 548) literally (case sensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
Named Capture Group SenodsFraction
(?P<SenodsFraction>\d{6}|\d{3})
1st Alternative
\d{6}
\d
matches a digit (equivalent to [0-9])
{6} matches the previous token exactly 6 times
2nd Alternative
\d{3}
\d
matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
Non-capturing group
(?: \w\W)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
\w matches any word character (equivalent to [a-zA-Z0-9_])
\W matches any non-word character (equivalent to [^a-zA-Z0-9_])
Non-capturing group
(?:(?:[\. \w+]?(?:(?P<Z>(?:[a-zA-Z]{3,5})|(?:[+-]\d\d?:?[0,3,4]?[0,5]?))|(?P<s>PD|[apmAPM]{2})))+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Non-capturing group
(?:[\. \w+]?(?:(?P<Z>(?:[a-zA-Z]{3,5})|(?:[+-]\d\d?:?[0,3,4]?[0,5]?))|(?P<s>PD|[apmAPM]{2})))+
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[\. \w+]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
\w matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the character + with index 4310 (2B16 or 538) literally (case sensitive)
Non-capturing group
(?:(?P<Z>(?:[a-zA-Z]{3,5})|(?:[+-]\d\d?:?[0,3,4]?[0,5]?))|(?P<s>PD|[apmAPM]{2}))
1st Alternative
(?P<Z>(?:[a-zA-Z]{3,5})|(?:[+-]\d\d?:?[0,3,4]?[0,5]?))
Named Capture Group Z
(?P<Z>(?:[a-zA-Z]{3,5})|(?:[+-]\d\d?:?[0,3,4]?[0,5]?))
1st Alternative
(?:[a-zA-Z]{3,5})
Non-capturing group
(?:[a-zA-Z]{3,5})
Match a single character present in the list below
[a-zA-Z]
{3,5} matches the previous token between 3 and 5 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 sensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
2nd Alternative
(?:[+-]\d\d?:?[0,3,4]?[0,5]?)
Non-capturing group
(?:[+-]\d\d?:?[0,3,4]?[0,5]?)
Match a single character present in the list below
[+-]
+-
matches a single character in the list +- (case sensitive)
\d matches a digit (equivalent to [0-9])
\d
matches a digit (equivalent to [0-9])
:
matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Match a single character present in the list below
[0,3,4]
Match a single character present in the list below
[0,5]
2nd Alternative
(?P<s>PD|[apmAPM]{2})
Named Capture Group s
(?P<s>PD|[apmAPM]{2})
1st Alternative
PD
PD
matches the characters PD literally (case sensitive)
2nd Alternative
[apmAPM]{2}
Global pattern flags
g modifier: global. All matches (don't return after first match)
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
g

Test String