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 (24)
~
^\s*\#?\s*(?:(?:(?'mins'[0-5]?\d)(?:[-,](?&mins))*)|\*)(?:/\d{1,2})?\s+(?:(?:(?'hours'(?:2[0-3]|[01]?\d))(?:[-,](?&hours))*)|\*)(?:/\d{1,2})?\s+(?:(?:(?'dmon'(?:3[01]|[12]?\d))(?:[-,](?&dmon))*)|\*)(?:/\d{1,2})?\s+(?:(?:(?'mon'(?:1[0-2]|[1-9]))(?:[-,](?&mon))*)|\*)(?:/\d{1,2})?\s+(?:(?:(?'dow'(?:[0-6]|\b(?:mon|tue|wed|thu|fri|sat|sun)\b))(?:[-,](?&dow))*)|\*)(?:/\d{1,2})?\s+.+$
~
^ asserts position at start of the string
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\#
matches the character # with index 3510 (2316 or 438) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Non-capturing group
(?:(?:(?'mins'[0-5]?\d)(?:[-,](?&mins))*)|\*)
1st Alternative
(?:(?'mins'[0-5]?\d)(?:[-,](?&mins))*)
Non-capturing group
(?:(?'mins'[0-5]?\d)(?:[-,](?&mins))*)
Named Capture Group mins
(?'mins'[0-5]?\d)
Match a single character present in the list below
[0-5]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
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
(?:[-,](?&mins))*
* 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 4510 (2D16 or 558) literally (case sensitive)
This hyphen is treated literally, which might be confusing for others. Consider escaping it or placing at the start or end of the class!
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
(?&mins) recurses the subpattern named mins
2nd Alternative
\*
\* matches the character * with index 4210 (2A16 or 528) literally (case sensitive)
Non-capturing group
(?:/\d{1,2})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\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)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Non-capturing group
(?:(?:(?'hours'(?:2[0-3]|[01]?\d))(?:[-,](?&hours))*)|\*)
1st Alternative
(?:(?'hours'(?:2[0-3]|[01]?\d))(?:[-,](?&hours))*)
Non-capturing group
(?:(?'hours'(?:2[0-3]|[01]?\d))(?:[-,](?&hours))*)
Named Capture Group hours
(?'hours'(?:2[0-3]|[01]?\d))
Non-capturing group
(?:2[0-3]|[01]?\d)
1st Alternative
2[0-3]
2 matches the character 2 with index 5010 (3216 or 628) literally (case sensitive)
Match a single character present in the list below
[0-3]
0-3 matches a single character in the range between 0 (index 48) and 3 (index 51) (case sensitive)
2nd Alternative
[01]?\d
Match a single character present in the list below
[01]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
01
matches a single character in the list 01 (case sensitive)
\d matches a digit (equivalent to [0-9])
Non-capturing group
(?:[-,](?&hours))*
* 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 4510 (2D16 or 558) literally (case sensitive)
This hyphen is treated literally, which might be confusing for others. Consider escaping it or placing at the start or end of the class!
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
(?&hours) recurses the subpattern named hours
2nd Alternative
\*
\* matches the character * with index 4210 (2A16 or 528) literally (case sensitive)
Non-capturing group
(?:/\d{1,2})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\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)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Non-capturing group
(?:(?:(?'dmon'(?:3[01]|[12]?\d))(?:[-,](?&dmon))*)|\*)
1st Alternative
(?:(?'dmon'(?:3[01]|[12]?\d))(?:[-,](?&dmon))*)
Non-capturing group
(?:(?'dmon'(?:3[01]|[12]?\d))(?:[-,](?&dmon))*)
Named Capture Group dmon
(?'dmon'(?:3[01]|[12]?\d))
Non-capturing group
(?:3[01]|[12]?\d)
1st Alternative
3[01]
3 matches the character 3 with index 5110 (3316 or 638) literally (case sensitive)
Match a single character present in the list below
[01]
01
matches a single character in the list 01 (case sensitive)
2nd Alternative
[12]?\d
Match a single character present in the list below
[12]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
12
matches a single character in the list 12 (case sensitive)
\d matches a digit (equivalent to [0-9])
Non-capturing group
(?:[-,](?&dmon))*
* 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 4510 (2D16 or 558) literally (case sensitive)
This hyphen is treated literally, which might be confusing for others. Consider escaping it or placing at the start or end of the class!
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
(?&dmon) recurses the subpattern named dmon
2nd Alternative
\*
\* matches the character * with index 4210 (2A16 or 528) literally (case sensitive)
Non-capturing group
(?:/\d{1,2})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\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)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
Non-capturing group
(?:(?:(?'mon'(?:1[0-2]|[1-9]))(?:[-,](?&mon))*)|\*)
Non-capturing group
(?:/\d{1,2})?
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
Non-capturing group
(?:(?:(?'dow'(?:[0-6]|\b(?:mon|tue|wed|thu|fri|sat|sun)\b))(?:[-,](?&dow))*)|\*)
Non-capturing group
(?:/\d{1,2})?
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
.
matches any character (except for line terminators)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

Regular Expression

~
~

Unit Tests

Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test
Unspecified unit test