Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
gi

Test String

Code Generator

Generated Code

use strict; my $str = '<form method=\'get\' action=\'/cgi-bin/webcmd\'><pre><input type=\'hidden\' name=\'screen\' value=\'Qam\'>Which EdgeQam (192.168.6.1+chassis): <input type=\'text\' name=\'Qam\' value=\'192.168.6.2\'> <input type=\'submit\'> <input type=\'submit\' name=\'qamLog\' value=\'qamLog\'></pre></form> <h2>Mega-Bits Per Second</h2>Port = QamChannel * 16 + QamSubChannel <table border cellspacing=0 cellpadding=5> <tr><td></td><td>PortBase</td><td>Chan</td><td>-1</td><td>-2</td><td>-3</td><td>-4</td><td>total</td></tr><tr><td>Qam1</td> <td>16</td><td>23</td><td>4.7</td><td>4.5</td><td>6.3</td><td>0.1</td><td>15.6</td></tr> <tr><td>Qam2</td> <td>32</td><td>24</td><td>6.3</td><td>8.0</td><td>7.3</td><td>0.2</td><td>21.8</td></tr> <tr><td>Qam3</td> <td>48</td><td>25</td><td>5.6</td><td>9.9</td><td>6.0</td><td>0.2</td><td>21.6</td></tr> <tr><td>Qam4</td> <td>64</td><td>26</td><td>5.5</td><td>7.2</td><td>6.8</td><td>0.2</td><td>19.6</td></tr> <tr><td>Qam5</td> <td>80</td><td>27</td><td>8.8</td><td>4.5</td><td>7.4</td><td>0.2</td><td>21.0</td></tr> <tr><td>Qam6</td> <td>96</td><td>28</td><td>8.4</td><td>5.1</td><td>7.5</td><td>0.2</td><td>21.3</td></tr> <tr><td>Qam7</td> <td>112</td><td>29</td><td>5.9</td><td>4.5</td><td>4.5</td><td>0.2</td><td>15.0</td></tr> <tr><td>Qam8</td> <td>128</td><td>30</td><td>4.9</td><td>1.1</td><td>4.5</td><td>0.2</td><td>10.8</td></tr> <tr><td>Qam9</td> <td>144</td><td>31</td><td>4.4</td><td>4.4</td><td>4.5</td><td>0.3</td><td>13.7</td></tr> <tr><td>Qam10</td> <td>160</td><td>32</td><td>8.4</td><td>5.0</td><td>8.8</td><td>0.2</td><td>22.3</td></tr> <tr><td>Qam11</td> <td>176</td><td>OFF</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td></tr> <tr><td>Qam12</td> <td>192</td><td>OFF</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td></tr> <tr><td>Total</td><td></td><td></td><td></td><td></td><td></td><td></td><td>182.8</td></tr></table> <h2>Control</h2>chassisId = 1, hwVersion = 0.16, swVersion = 1.11.9, tempC = 40 MAC = c4:27:95:98:ed:eb, licenseCount = 10 <form method=\'get\' action=\'/cgi-bin/webcmd\'><pre><input type=\'hidden\' name=\'screen\' value=\'qamControlWrite\'><input type=\'hidden\' name=\'qamIp\' value=\'192.168.6.2\'><input type=\'hidden\' name=\'modulationMode\' value=\'1\'><input type=\'hidden\' name=\'qamMode\' value=\'4\'>baseChannel: A=<input type=\'text\' name=\'baseChannel0\' value=\'23\' size=\'3\'> B=<input type=\'text\' name=\'baseChannel1\' value=\'27\' size=\'3\'> C=<input type=\'text\' name=\'baseChannel2\' value=\'31\' size=\'3\'> <input type=\'hidden\' name=\'strengthPercent0\' value=\'95\' size=\'3\'><input type=\'hidden\' name=\'strengthPercent1\' value=\'95\' size=\'3\'><input type=\'hidden\' name=\'strengthPercent2\' value=\'95\' size=\'3\'>carrierOn: A=<input type=\'text\' name=\'carrierOn0\' value=\'1\' size=\'1\'><input type=\'text\' name=\'carrierOn1\' value=\'1\' size=\'1\'><input type=\'text\' name=\'carrierOn2\' value=\'1\' size=\'1\'><input type=\'text\' name=\'carrierOn3\' value=\'1\' size=\'1\'> B=<input type=\'text\' name=\'carrierOn4\' value=\'1\' size=\'1\'><input type=\'text\' name=\'carrierOn5\' value=\'1\' size=\'1\'><input type=\'text\' name=\'carrierOn6\' value=\'1\' size=\'1\'><input type=\'text\' name=\'carrierOn7\' value=\'1\' size=\'1\'> C=<input type=\'text\' name=\'carrierOn8\' value=\'1\' size=\'1\'><input type=\'text\' name=\'carrierOn9\' value=\'1\' size=\'1\'><input type=\'text\' name=\'carrierOn10\' value=\'0\' size=\'1\'><input type=\'text\' name=\'carrierOn11\' value=\'0\' size=\'1\'> alternateIp: <input type=\'text\' name=\'alternateIp\' value=\'192.168.6.99\'> <input type=\'hidden\' name=\'macAddress\' value=\'c4:27:95:98:ed:eb\'>tftpIp: <input type=\'text\' name=\'tftpIp\' value=\'192.168.100.3\'> tftpFilename: <input type=\'text\' name=\'tftpFilename\' value=\'\' size=\'60\'> reset: <input type=\'text\' name=\'reset\' value=\'0\'> <input type=\'submit\'> </pre></form>'; my $regex = qr/\<td\>(.{2,5})\<\/td\>/ip; if ( $str =~ /$regex/g ) { print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n"; # print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n"; # print "Capture Group 2 is $2 ... and so on\n"; } # ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p' # Named capture groups can be called via $+{name}

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 Perl, please visit: http://perldoc.perl.org/perlre.html