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
`
(?:implements|new|@var|extends)\s*(?P<fullNamespace>\\[A-Za-z]+|[A-Za-z]+?(?:\\(?P<class>[A-Za-z]+))+)
`
g
Non-capturing group
(?:implements|new|@var|extends)
1st Alternative
implements
implements
matches the characters implements literally (case sensitive)
2nd Alternative
new
new
matches the characters new literally (case sensitive)
3rd Alternative
@var
@var
matches the characters @var literally (case sensitive)
4th Alternative
extends
extends
matches the characters extends literally (case sensitive)
\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)
Named Capture Group fullNamespace
(?P<fullNamespace>\\[A-Za-z]+|[A-Za-z]+?(?:\\(?P<class>[A-Za-z]+))+)
1st Alternative
\\[A-Za-z]+
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
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 65) and Z (index 90) (case sensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
2nd Alternative
[A-Za-z]+?(?:\\(?P<class>[A-Za-z]+))+
Match a single character present in the list below
[A-Za-z]
+? matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
Non-capturing group
(?:\\(?P<class>[A-Za-z]+))+
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
Named Capture Group class
(?P<class>[A-Za-z]+)
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 65) and Z (index 90) (case sensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
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

`
`
g

Test String