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
  • 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

/
/
gmx

Test String

Substitution

Processing...

Code Generator

Generated Code

$re = '/^[#].*\n*(*SKIP)(*F)| # Version 1 (substitution: \'$1$2\n\'): # Match 1-80 characters, backtracking as necessary until whitespace or # EOL is reached. If words > 80 characters (w/o whitespace) are found, # no wrapping is performed on that word. # [1353 steps] #(.{1,80})(?:\s|$)|(\S{81,})(?:\s|$) # Version 2 (substitution: \'$1$2\n\'): # Same as Version 1 but with a negative look-ahead at the front to skip # to the alternate branch sooner. # [773 steps] #(?!\S{81})(.{1,80})(?:\s|$)|(\S{81,})(?:\s|$) # Version 3 (substitution: \'$1$2\n\'): # Same as Version 1 but alternations swapped. # [749 steps] #(\S{81,})(?:\s|$)|(.{1,80})(?:\s|$) # Version 4 (substitution: \'$1\n\'): # Combine alternations into one group. # [712 steps] (\S{79,}|.{1,80})(?:\s|$) /mx'; $str = 'THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #### The following should remain on the same line (not wrap): # Multiple words whose total character+whitespace count <= 80 123 5678 0 23 5678 012 45678 0 2345 789 12345 78901 3456 8901 3 56 89 12345 7890 # 78-character word followed by 1-character word 123456789012345678901234567890123456789012345678901234567890123456789012345678 1 # 1-character word followed by 78-character word 1 345678901234567890123456789012345678901234567890123456789012345678901234567890 #### The following should wrap to the next line: # 45x 4-character words with 2 spaces between each. 1234 7890 3456 9012 5678 1234 7890 3456 9012 5678 1234 7890 3456 9012 5678 1234 7890 3456 9012 5678 1234 7890 3456 9012 5678 1234 7890 3456 9012 5678 1234 7890 3456 9012 5678 1234 7890 3456 9012 5678 1234 7890 3456 9012 5678 # 79-character word followed by 1-character word 1234567890123456789012345678901234567890123456789012345678901234567890123456789 1 # 1-character word followed by 79-character word 1 3456789012345678901234567890123456789012345678901234567890123456789012345678901 # 80-character words followed by >80-character words: 12345678901234567890123456789012345678901234567890123456789012345678901234567890 123456789012345678901234567890123456789012345678901234567890123456789012345678901 12345678901234567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890123456789012345678901234567890123456789012 '; $subst = "$1\n"; $result = preg_replace($re, $subst, $str); echo "The result of the substitution is ".$result;

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