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
/
((?:(?# path without "" )(?<opening>(?# 'C:/', '//' or '%VariableWindows%')(?<montage>\b[a-zA-Z]:[\/\\])|(?:[\/\\][\/\\])|(?:%\w+%[\/\\]?))(?:(?# directory btw // )[^\/\\<>:"|?\n\r]+[\/\\])*(?:(?#we search for fileName only if next character is pathFriendly)(?=[^\/\\<>:"'|?\n\r;, ])(?:(?#name file ? dir ?)(?:[^\/\\<>:"|?\n\r;, .](?: (?=\w))?(?:\*(?!= ))?(?!(?&montage)))+)?(?:(?#extention)\.\w+)*))|(?:(?# path quoted "" or '' )["'](?&opening)(?=.*?["'])(?:[^\/\\<>:'"|?\n\r]+[\/\\]?)+?["']))
/
g
1st Capturing Group
((?:(?# path without "" )(?<opening>(?# 'C:/', '//' or '%VariableWindows%')(?<montage>\b[a-zA-Z]:[\/\\])|(?:[\/\\][\/\\])|(?:%\w+%[\/\\]?))(?:(?# directory btw // )[^\/\\<>:"|?\n\r]+[\/\\])*(?:(?#we search for fileName only if next character is pathFriendly)(?=[^\/\\<>:"'|?\n\r;, ])(?:(?#name file ? dir ?)(?:[^\/\\<>:"|?\n\r;, .](?: (?=\w))?(?:\*(?!= ))?(?!(?&montage)))+)?(?:(?#extention)\.\w+)*))|(?:(?# path quoted "" or '' )["'](?&opening)(?=.*?["'])(?:[^\/\\<>:'"|?\n\r]+[\/\\]?)+?["']))
1st Alternative
(?:(?# path without "" )(?<opening>(?# 'C:/', '//' or '%VariableWindows%')(?<montage>\b[a-zA-Z]:[\/\\])|(?:[\/\\][\/\\])|(?:%\w+%[\/\\]?))(?:(?# directory btw // )[^\/\\<>:"|?\n\r]+[\/\\])*(?:(?#we search for fileName only if next character is pathFriendly)(?=[^\/\\<>:"'|?\n\r;, ])(?:(?#name file ? dir ?)(?:[^\/\\<>:"|?\n\r;, .](?: (?=\w))?(?:\*(?!= ))?(?!(?&montage)))+)?(?:(?#extention)\.\w+)*))
Non-capturing group
(?:(?# path without "" )(?<opening>(?# 'C:/', '//' or '%VariableWindows%')(?<montage>\b[a-zA-Z]:[\/\\])|(?:[\/\\][\/\\])|(?:%\w+%[\/\\]?))(?:(?# directory btw // )[^\/\\<>:"|?\n\r]+[\/\\])*(?:(?#we search for fileName only if next character is pathFriendly)(?=[^\/\\<>:"'|?\n\r;, ])(?:(?#name file ? dir ?)(?:[^\/\\<>:"|?\n\r;, .](?: (?=\w))?(?:\*(?!= ))?(?!(?&montage)))+)?(?:(?#extention)\.\w+)*))
Comment: path without ""
Named Capture Group opening
(?<opening>(?# 'C:/', '//' or '%VariableWindows%')(?<montage>\b[a-zA-Z]:[\/\\])|(?:[\/\\][\/\\])|(?:%\w+%[\/\\]?))
1st Alternative
(?# 'C:/', '//' or '%VariableWindows%')(?<montage>\b[a-zA-Z]:[\/\\])
Comment: 'C:/', '//' or '%VariableWindows%'
Named Capture Group montage
(?<montage>\b[a-zA-Z]:[\/\\])
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
Match a single character present in the list below
[a-zA-Z]
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Match a single character present in the list below
[\/\\]
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
2nd Alternative
(?:[\/\\][\/\\])
Non-capturing group
(?:[\/\\][\/\\])
Match a single character present in the list below
[\/\\]
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
Match a single character present in the list below
[\/\\]
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
3rd Alternative
(?:%\w+%[\/\\]?)
Non-capturing group
(?:%\w+%[\/\\]?)
% matches the character % with index 3710 (2516 or 458) literally (case sensitive)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
% matches the character % with index 3710 (2516 or 458) literally (case sensitive)
Match a single character present in the list below
[\/\\]
? 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)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
Non-capturing group
(?:(?# directory btw // )[^\/\\<>:"|?\n\r]+[\/\\])*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Comment: directory btw //
Match a single character not present in the list below
[^\/\\<>:"|?\n\r]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
<>:"|?
matches a single character in the list <>:"|? (case sensitive)
\n matches a line-feed (newline) character (ASCII 10)
\r matches a carriage return (ASCII 13)
Match a single character present in the list below
[\/\\]
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
Non-capturing group
(?:(?#we search for fileName only if next character is pathFriendly)(?=[^\/\\<>:"'|?\n\r;, ])(?:(?#name file ? dir ?)(?:[^\/\\<>:"|?\n\r;, .](?: (?=\w))?(?:\*(?!= ))?(?!(?&montage)))+)?(?:(?#extention)\.\w+)*)
Comment: we search for fileName only if next character is pathFriendly
Positive Lookahead
(?=[^\/\\<>:"'|?\n\r;, ])
Assert that the Regex below matches
Match a single character not present in the list below
[^\/\\<>:"'|?\n\r;, ]
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
<>:"'|?
matches a single character in the list <>:"'|? (case sensitive)
\n matches a line-feed (newline) character (ASCII 10)
\r matches a carriage return (ASCII 13)
;,
matches a single character in the list ;, (case sensitive)
Non-capturing group
(?:(?#name file ? dir ?)(?:[^\/\\<>:"|?\n\r;, .](?: (?=\w))?(?:\*(?!= ))?(?!(?&montage)))+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Comment: name file ? dir ?
Non-capturing group
(?:[^\/\\<>:"|?\n\r;, .](?: (?=\w))?(?:\*(?!= ))?(?!(?&montage)))+
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character not present in the list below
[^\/\\<>:"|?\n\r;, .]
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
<>:"|?
matches a single character in the list <>:"|? (case sensitive)
\n matches a line-feed (newline) character (ASCII 10)
\r matches a carriage return (ASCII 13)
;, .
matches a single character in the list ;, . (case sensitive)
Non-capturing group
(?: (?=\w))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Positive Lookahead
(?=\w)
Assert that the Regex below matches
\w matches any word character (equivalent to [a-zA-Z0-9_])
Non-capturing group
(?:\*(?!= ))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\* matches the character * with index 4210 (2A16 or 528) literally (case sensitive)
Negative Lookahead
(?!= )
Assert that the Regex below does not match
=
matches the characters = literally (case sensitive)
Negative Lookahead
(?!(?&montage))
Assert that the Regex below does not match
(?&montage) recurses the subpattern named montage
Non-capturing group
(?:(?#extention)\.\w+)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Comment: extention
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Alternative
(?:(?# path quoted "" or '' )["'](?&opening)(?=.*?["'])(?:[^\/\\<>:'"|?\n\r]+[\/\\]?)+?["'])
Non-capturing group
(?:(?# path quoted "" or '' )["'](?&opening)(?=.*?["'])(?:[^\/\\<>:'"|?\n\r]+[\/\\]?)+?["'])
Comment: path quoted "" or ''
Match a single character present in the list below
["']
"'
matches a single character in the list "' (case sensitive)
(?&opening) recurses the subpattern named opening
Positive Lookahead
(?=.*?["'])
Assert that the Regex below matches
.
matches any character (except for line terminators)
*? matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
Match a single character present in the list below
["']
"'
matches a single character in the list "' (case sensitive)
Non-capturing group
(?:[^\/\\<>:'"|?\n\r]+[\/\\]?)+?
+? matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)
Match a single character not present in the list below
[^\/\\<>:'"|?\n\r]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
<>:'"|?
matches a single character in the list <>:'"|? (case sensitive)
\n matches a line-feed (newline) character (ASCII 10)
\r matches a carriage return (ASCII 13)
Match a single character present in the list below
[\/\\]
? 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)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
Match a single character present in the list below
["']
"'
matches a single character in the list "' (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.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
g

Test String