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
/
^(?(DEFINE)(?'dn'[^\\\/:*"<>|\ ]+[^\\\/:*"<>|]*[^\\\/:*"<>|\.\ ]+)(?'ds'\\|\/))(?'fn_path'(?'f_path'(?&ds)?((\.{1,2}(?&ds))|((?&dn)(?&ds)))*)?(?'fn'(?&dn)))(?'ext_dot'\.(?'ext'asm))$
/
gm
^ asserts position at start of a line
Subpattern definition construct
(?(DEFINE)(?'dn'[^\\\/:*"<>|\ ]+[^\\\/:*"<>|]*[^\\\/:*"<>|\.\ ]+)(?'ds'\\|\/))
May only be used to define functions. No matching is done in this group.
Named Capture Group dn
(?'dn'[^\\\/:*"<>|\ ]+[^\\\/:*"<>|]*[^\\\/:*"<>|\.\ ]+)
Match a single character not present in the list below
[^\\\/:*"<>|\ ]
+ 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)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
:*"<>|
matches a single character in the list :*"<>| (case sensitive)
\ matches the character with index 3210 (2016 or 408) literally (case sensitive)
Match a single character not present in the list below
[^\\\/:*"<>|]
* matches the previous token between zero 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)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
:*"<>|
matches a single character in the list :*"<>| (case sensitive)
Match a single character not present in the list below
[^\\\/:*"<>|\.\ ]
+ 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)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
:*"<>|
matches a single character in the list :*"<>| (case sensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\ matches the character with index 3210 (2016 or 408) literally (case sensitive)
Named Capture Group ds
(?'ds'\\|\/)
1st Alternative
\\
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
2nd Alternative
\/
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Named Capture Group fn_path
(?'fn_path'(?'f_path'(?&ds)?((\.{1,2}(?&ds))|((?&dn)(?&ds)))*)?(?'fn'(?&dn)))
Named Capture Group f_path
(?'f_path'(?&ds)?((\.{1,2}(?&ds))|((?&dn)(?&ds)))*)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
(?&ds)
recurses the subpattern named ds
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
5th Capturing Group
((\.{1,2}(?&ds))|((?&dn)(?&ds)))*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
1st Alternative
(\.{1,2}(?&ds))
6th Capturing Group
(\.{1,2}(?&ds))
\.
matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
(?&ds) recurses the subpattern named ds
2nd Alternative
((?&dn)(?&ds))
7th Capturing Group
((?&dn)(?&ds))
(?&dn) recurses the subpattern named dn
(?&ds) recurses the subpattern named ds
Named Capture Group fn
(?'fn'(?&dn))
(?&dn) recurses the subpattern named dn
Named Capture Group ext_dot
(?'ext_dot'\.(?'ext'asm))
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
Named Capture Group ext
(?'ext'asm)
asm
matches the characters asm literally (case sensitive)
$ 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)
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
gm

Test String