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
/
^(?:(?<!\/\/|\/\*|<!--).)* #check if commented out <[a-z][a-z0-9]*?\b #find start of a tag (?:.(?![^?]>))+ #Must not find > between start and class unless ?> class=(["']) #Find class and opening quotes (?:(?!\1).)* \bmatch\b (?:(?!\1).)* #see if our tag is in between the quotes \1 #closing quotes .*(?<!\?)> #find first > without ? in front of it
/
gmUix
^ asserts position at start of a line
Non-capturing group
(?:(?<!\/\/|\/\*|<!--).)*
* matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
Negative Lookbehind
(?<!\/\/|\/\*|<!--)
Assert that the Regex below does not match
1st Alternative
\/\/
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
2nd Alternative
\/\*
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
\* matches the character * with index 4210 (2A16 or 528) literally (case insensitive)
3rd Alternative
<!--
<!--
matches the characters <!-- literally (case insensitive)
. matches any character (except for line terminators)
#check if commented out <
check if commented out
Comment: check if commented out
< matches the character < with index 6010 (3C16 or 748) literally (case insensitive)
Match a single character present in the list below
[a-z]
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
Match a single character present in the list below
[a-z0-9]
*? matches the previous token between zero 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)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
#find start of a tag
find start of a tag
Comment: find start of a tag
Non-capturing group
(?:.(?![^?]>))+
+ matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)
. matches any character (except for line terminators)
Negative Lookahead
(?![^?]>)
Assert that the Regex below does not match
Match a single character not present in the list below
[^?]
? matches the character ? with index 6310 (3F16 or 778) literally (case insensitive)
> matches the character > with index 6210 (3E16 or 768) literally (case insensitive)
#Must not find > between start and class unless ?> class=
Must not find > between start and class unless ?>
Comment: Must not find > between start and class unless ?>
c matches the character c with index 9910 (6316 or 1438) literally (case insensitive)
l matches the character l with index 10810 (6C16 or 1548) literally (case insensitive)
a matches the character a with index 9710 (6116 or 1418) literally (case insensitive)
s matches the character s with index 11510 (7316 or 1638) literally (case insensitive)
s matches the character s with index 11510 (7316 or 1638) literally (case insensitive)
= matches the character = with index 6110 (3D16 or 758) literally (case insensitive)
1st Capturing Group
(["'])
Match a single character present in the list below
["']
"'
matches a single character in the list "' (case insensitive)
#Find class and opening quotes
Find class and opening quotes
Comment: Find class and opening quotes
Non-capturing group
(?:(?!\1).)*
* matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
Negative Lookahead
(?!\1)
Assert that the Regex below does not match
\1 matches the same text as most recently matched by the 1st capturing group
. matches any character (except for line terminators)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
match
matches the characters match literally (case insensitive)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
Non-capturing group
(?:(?!\1).)*
#see if our tag is in between the quotes
see if our tag is in between the quotes
\1 matches the same text as most recently matched by the 1st capturing group
#closing quotes
closing quotes
.
matches any character (except for line terminators)
Negative Lookbehind
(?<!\?)
Assert that the Regex below does not match
>
matches the character > with index 6210 (3E16 or 768) literally (case insensitive)
Comment: find first > without ? in front of it
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)
U modifier: Ungreedy. The match becomes lazy by default. Now a ? following a quantifier makes it greedy
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.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
gmUix

Test String