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
/
^\s*(((\/\/|#)[^\n]*\n*)|(\/\*((?!\*\/)(.|\n))+\*\/)|\s)*\s*(\$|echo|class\s+\w|\/\/php|#php|mysql_|namespace|use|include|require|function\s+[^\(\s]+\s*\(\s*\$|((if|while|for|switch|foreach)\s*\(\s*\$\w))
/
i
^ asserts position at start of the string
\s
matches any whitespace character (equivalent to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
1st Capturing Group
(((\/\/|#)[^\n]*\n*)|(\/\*((?!\*\/)(.|\n))+\*\/)|\s)*
* 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
((\/\/|#)[^\n]*\n*)
2nd Capturing Group
((\/\/|#)[^\n]*\n*)
3rd Capturing Group
(\/\/|#)
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 3510 (2316 or 438) literally (case insensitive)
Match a single character not present in the list below
[^\n]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\n matches a line-feed (newline) character (ASCII 10)
\n
matches a line-feed (newline) character (ASCII 10)
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Alternative
(\/\*((?!\*\/)(.|\n))+\*\/)
4th Capturing Group
(\/\*((?!\*\/)(.|\n))+\*\/)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
\* matches the character * with index 4210 (2A16 or 528) literally (case insensitive)
5th Capturing Group
((?!\*\/)(.|\n))+
+ matches the previous token between one 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
Negative Lookahead
(?!\*\/)
Assert that the Regex below does not match
\* matches the character * with index 4210 (2A16 or 528) literally (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
6th Capturing Group
(.|\n)
1st Alternative
.
. matches any character (except for line terminators)
2nd Alternative
\n
\n matches a line-feed (newline) character (ASCII 10)
\* matches the character * with index 4210 (2A16 or 528) literally (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
3rd Alternative
\s
\s matches any whitespace character (equivalent to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
\s
matches any whitespace character (equivalent to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
7th Capturing Group
(\$|echo|class\s+\w|\/\/php|#php|mysql_|namespace|use|include|require|function\s+[^\(\s]+\s*\(\s*\$|((if|while|for|switch|foreach)\s*\(\s*\$\w))
1st Alternative
\$
\$ matches the character $ with index 3610 (2416 or 448) literally (case insensitive)
2nd Alternative
echo
echo
matches the characters echo literally (case insensitive)
3rd Alternative
class\s+\w
class
matches the characters class literally (case insensitive)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
4th Alternative
\/\/php
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
php
matches the characters php literally (case insensitive)
5th Alternative
#php
#php
matches the characters #php literally (case insensitive)
6th Alternative
mysql_
mysql_
matches the characters mysql_ literally (case insensitive)
7th Alternative
namespace
namespace
matches the characters namespace literally (case insensitive)
8th Alternative
use
use
matches the characters use literally (case insensitive)
9th Alternative
include
10th Alternative
require
11th Alternative
function\s+[^\(\s]+\s*\(\s*\$
12th Alternative
((if|while|for|switch|foreach)\s*\(\s*\$\w)
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
Your regular expression does not match the subject string.

Regular Expression
No Match

/
/
i

Test String

Code Generator

Generated Code

Loading code sample...
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report.
For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions