Regular Expressions 101

Save & Share

  • Regex Version: ver. 4
  • Update Regex
    ctrl+⇧+s
  • Save new 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
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

/
/
gx

Test String

Substitution

Processing...

Code Generator

Generated Code

$re = '/(?(DEFINE) # Construct a definition structure (?<punc>[!?.]+) # Define `punc` group consisting of `.`, `?` and `!` ) # End of definition \b # Match a word boundary position (?> # Open a grouping (non-capturing) (a) [a-z0-9] # Match a digit or a lower case letter \w* # And any number of word characters | # Or [A-Z] # Match a upper case letter \w{3,} # And word characters more than 3 (?= # Followed by (?&punc) # Any number of `.`, `?` and `!` characters ) # End of positive lookahead ) # End of grouping (a) (?&punc) # Match any number of `.`, `?` and `!` characters \K\B\s* # Reset match, assert a NWB position + any number of whitespaces/x'; $str = 'Fame came to Gama at the age of 17 when he challenged then-Indian Wrestling Champion, middle-aged Raheem Bakhsh Sultani Wala, another Muslim wrestler from Gujranwala, now in Punjab, Pakistan. At about 7 feet tall, with a very impressive win-loss record, Raheem was expected to easily defeat the 5\'7" G. Raheem\'s only drawback was his age as he was much older than Gama, and near the end of his career. The bout continued for hours and eventually ended in a draw. The contest with Raheem was the turning point in Gama\'s career. After that, he was looked upon as the next contender for the Indian Wrestling Championship. In the first bout Gama remained defensive, but in the second bout, Gama went on the offensive. Despite severe bleeding from his nose and ears, he managed to deal out a great deal of damage to Raheem Bakhsh. By 1910, Gama had defeated all the prominent Indian wrestlers who faced him except the champion, Raheem Bakhsh Sultani Wala. At this time, he focused his attention on the rest of the world. Accompanied by his younger brother Imam Bakhsh, Gama sailed to England to compete with the Western Wrestlers but could not gain instant entry, because of his lower height.[6] In London, Gama issued a challenge that he could throw any three wrestlers in thirty minutes of any weight class. This announcement however was seen as a bluff by the wrestlers and their wrestling promoter R. B. Benjamin. For a long time no one came forward to accept the challenge. To break the ice, Gama presented another challenge to specific heavy weight wrestlers. He challenged Stanislaus Zbyszko and Frank Gotch, either he would beat them or pay them the prize money and go home. The first professional wrestler to take his challenge was the American Benjamin Roller. In the bout, Gama pinned Roller in 1 minute 40 seconds the first time, and in 9 minutes 10 seconds the other. On the second day, he defeated 12 wrestlers and thus gained entry to official tournament.'; $subst = "\n\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