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
"
^\#\s* # must start with "#" to indicate beeing a repo. (?:@?@?(?!-)(?P<user>(?:[a-zA-Z0-9]|-(?!-))+[a-zA-Z0-9])(?![^\x00-\x7F]))? # Username part (?:(?<=\#)|[/\s\.](?<!\v))(?P<repo>[a-zA-Z0-9_.-]+)$
"
gmix
^ asserts position at start of a line
\# matches the character # with index 3510 (2316 or 438) literally (case insensitive)
\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)
# must start with "#" to indicate beeing a repo.
must start with "#" to indicate beeing a repo.
Comment: must start with "#" to indicate beeing a repo.
Non-capturing group
(?:@?@?(?!-)(?P<user>(?:[a-zA-Z0-9]|-(?!-))+[a-zA-Z0-9])(?![^\x00-\x7F]))?
? 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)
@
matches the character @ with index 6410 (4016 or 1008) literally (case insensitive)
Negative Lookahead
(?!-)
Assert that the Regex below does not match
- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
Named Capture Group user
(?P<user>(?:[a-zA-Z0-9]|-(?!-))+[a-zA-Z0-9])
Non-capturing group
(?:[a-zA-Z0-9]|-(?!-))+
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
1st Alternative
[a-zA-Z0-9]
Match a single character present in the list below
[a-zA-Z0-9]
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
2nd Alternative
-(?!-)
- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
Negative Lookahead
(?!-)
Assert that the Regex below does not match
- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
Match a single character present in the list below
[a-zA-Z0-9]
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
Negative Lookahead
(?![^\x00-\x7F])
Assert that the Regex below does not match
Match a single character not present in the list below
[^\x00-\x7F]
\x00-\x7F matches a single character in the range between (index 0) and  (index 127) (case insensitive)
# Username part
Username part
Comment: Username part
Non-capturing group
(?:(?<=\#)|[/\s\.](?<!\v))
1st Alternative
(?<=\#)
Positive Lookbehind
(?<=\#)
Assert that the Regex below matches
\# matches the character # with index 3510 (2316 or 438) literally (case insensitive)
2nd Alternative
[/\s\.](?<!\v)
Match a single character present in the list below
[/\s\.]
/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v  ])
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
Negative Lookbehind
(?<!\v)
Assert that the Regex below does not match
\v matches a vertical tab character
Named Capture Group repo
(?P<repo>[a-zA-Z0-9_.-]+)
Match a single character present in the list below
[a-zA-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)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (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)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
x modifier: extended. Spaces and text after a # in the pattern are ignored
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
gmix

Test String