Regular Expressions 101

Save & Share

  • Save new Regex
    ctrl+s
  • Update Regex
    ctrl+⇧+s
  • Add to Community Library

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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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

~
~
gs

Test String

Code Generator

Generated Code

$re = '~<tr>(?:.(?!<tr>))*?<td>.*?</td>(?:.(?!<tr>))*?<td>(.*?)</td>~s'; $str = '<link href="http://www.mhpa.co.uk/reports.css" rel="STYLESHEET" type="text/css" CHARSET="ISO-8859-1"> <table cellpadding=2 cellspacing=0 border=0 width=100%> <tbody> <tr> <td align=right><b>11/09/2013&nbsp;at&nbsp;09:48</b></td> </tr> </tbody> </table> <center> <table border="1" bordercolor="silver" cellpadding="2" cellspacing="0" width="100%"> <thead> <tr> <th width=100>ETA</th> <th width=100>Ship Name</th> <th width=80>From port</th> <th width=80>To berth</th> <th width=130>Agent</th> </tr> </thead> <tbody> <tr><td>11/09/2013 at 09:00&nbsp;</td> <td>SONANGOL KALANDULA&nbsp;</td> <td>Cabinda&nbsp;</td> <td>Valero 6&nbsp;</td> <td>Graypen&nbsp;</td> </tr> <tr> <td>11/09/2013 at 11:45&nbsp;</td> <td>ISLE OF INISHMORE&nbsp;</td> <td>Rosslare&nbsp;</td> <td>PDFT&nbsp;</td> <td>Irish Ferries&nbsp;</td> </tr> <tr> <td>11/09/2013 at 12:00&nbsp;</td> <td>BRO DEVELOPER&nbsp;</td> <td>Avonmouth&nbsp;</td> <td>Valero 8&nbsp;</td> <td>GAC&nbsp;</td> </tr> <tr> <td>11/09/2013 at 14:00&nbsp;</td> <td>WELSH PIPER&nbsp;</td> <td>Sea&nbsp;</td> <td>POP No 1&nbsp;</td> <td>Tarmac Marine Dredging Ltd&nbsp;</td> </tr> <tr> <td>11/09/2013 at 22:00&nbsp;</td> <td>PATRICIA&nbsp;</td> <td>Le Havre&nbsp;</td> <td>Murco 1&nbsp;</td> <td>GAC&nbsp;</td> </tr> <tr> <td>12/09/2013 at 05:00&nbsp;</td> <td>BRO DESIGNER&nbsp;</td> <td>Plymouth&nbsp;</td> <td>Murco 2&nbsp;</td> <td>Graypen&nbsp;</td> </tr> <tr> <td>12/09/2013 at 06:00&nbsp;</td> <td>BASTOGNE&nbsp;</td> <td>Mongstad&nbsp;</td> <td>Valero 3&nbsp;</td> <td>Denholm Wilhelmsen Ltd&nbsp;</td> </tr> <tr> <td>12/09/2013 at 06:00&nbsp;</td> <td>MINERVA GRACE&nbsp;</td> <td>Ras Lanuf&nbsp;</td> <td>Valero 1&nbsp;</td> <td>Graypen&nbsp;</td> </tr> <tr> <td>12/09/2013 at 06:00&nbsp;</td> <td>LEANDER&nbsp;</td> <td>Dublin &nbsp;</td> <td>Murco 2&nbsp;</td> <td>Graypen&nbsp;</td> </tr> <tr> <td>12/09/2013 at 07:00&nbsp;</td> <td>THUN GLOBE&nbsp;</td> <td>Cardiff&nbsp;</td> <td>Valero 7&nbsp;</td> <td>GAC&nbsp;</td> </tr> <tr> <td>13/09/2013 at 06:00&nbsp;</td> <td>THUN GENIUS&nbsp;</td> <td>Dublin &nbsp;</td> <td>Valero&nbsp;</td> <td>GAC&nbsp;</td> </tr> <tr> <td>13/09/2013 at 11:00&nbsp;</td> <td>CAP LEON&nbsp;</td> <td>Arzew&nbsp;</td> <td>Valero 6&nbsp;</td> <td>Graypen&nbsp;</td> </tr> <tr> <td>13/09/2013 at 19:00&nbsp;</td> <td>STOLT TEAL&nbsp;</td> <td>TBA&nbsp;</td> <td>Valero 1&nbsp;</td> <td>Cory Bros&nbsp;</td> </tr> <tr> <td>14/09/2013 at 16:00&nbsp;</td> <td>B GAS LANRICK&nbsp;</td> <td>Antwerp&nbsp;</td> <td>Murco 3&nbsp;</td> <td>S5 Agency World&nbsp;</td> </tr> <tr> <td>15/09/2013 at 09:00&nbsp;</td> <td>BAYAMO&nbsp;</td> <td>TBA&nbsp;</td> <td>Valero&nbsp;</td> <td>Graypen&nbsp;</td> </tr> <tr> <td>15/09/2013 at 09:00&nbsp;</td> <td>AMADEA&nbsp;</td> <td>Cork&nbsp;</td> <td>Milford Shelf&nbsp;</td> <td>Inchcape Shipping Services&nbsp;</td> </tr> <tr> <td>15/09/2013 at 12:00&nbsp;</td> <td>ARRAN&nbsp;</td> <td>Dublin &nbsp;</td> <td>Valero&nbsp;</td> <td>Graypen&nbsp;</td> </tr> <tr> <td>16/09/2013 at 01:00&nbsp;</td> <td>EKFJORD&nbsp;</td> <td>Gothenburg&nbsp;</td> <td>Murco 1&nbsp;</td> <td>Cory Bros&nbsp;</td> </tr> <tr> <td>16/09/2013 at 21:30&nbsp;</td> <td>MOZAH&nbsp;</td> <td>Ras Laffan&nbsp;</td> <td>South Hook No 1&nbsp;</td> <td>S5 Agency World&nbsp;</td> </tr> <tr> <td>17/09/2013 at 07:00&nbsp;</td> <td>STRIDE&nbsp;</td> <td>Murmansk&nbsp;</td> <td>Valero 6&nbsp;</td> <td>Graypen&nbsp;</td> </tr> </tbody> </table> </center>'; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php