Regular Expressions 101

Save & Share

  • Save Regex
    ctrl+s
  • Update Regex
    ctrl+⇧+s

Flavor

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

Function

  • Match
  • Substitution
  • List
  • Unit Tests
/
(?:(?P<inverted>\!) )?(?P<names>\-{1,2}\S+)($| (?P<values>.+(?=(?: [\!|\-])|$)))
/
gU
Non-capturing group
(?:(?P<inverted>\!) )?
? matches the previous token between zero and one times, as few times as possible, expanding as needed (lazy)
Named Capture Group inverted
(?P<inverted>\!)
\! matches the character ! with index 3310 (2116 or 418) literally (case sensitive)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Named Capture Group names
(?P<names>\-{1,2}\S+)
\-
matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
{1,2} matches the previous token between 1 and 2 times, as few times as possible, expanding as needed (lazy)
\S
matches any non-whitespace character (equivalent to [^\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)
3rd Capturing Group
($| (?P<values>.+(?=(?: [\!|\-])|$)))
1st Alternative
$
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
2nd Alternative
(?P<values>.+(?=(?: [\!|\-])|$))
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Named Capture Group values
(?P<values>.+(?=(?: [\!|\-])|$))
.
matches any character (except for line terminators)
+ matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)
Positive Lookahead
(?=(?: [\!|\-])|$)
Assert that the Regex below matches
1st Alternative
(?: [\!|\-])
Non-capturing group
(?: [\!|\-])
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Match a single character present in the list below
[\!|\-]
\! matches the character ! with index 3310 (2116 or 418) literally (case sensitive)
| matches the character | with index 12410 (7C16 or 1748) literally (case sensitive)
\- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
2nd Alternative
$
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
Global pattern flags
g modifier: global. All matches (don't return after first match)
U modifier: Ungreedy. The match becomes lazy by default. Now a ? following a quantifier makes it greedy
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
gU

Test String