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

/
/
gmU

Test String

Substitution

Processing...

Code Generator

Generated Code

$re = '/(<footer><!\[CDATA\[)\s?(<p>)?(.*)(<p>)/mU'; $str = '<?xml version="1.0" encoding="utf-8"?> <content> <title /> <meta-description /> <meta-robots /> <heading1 /> <canonical /> <footer><![CDATA[ If American cities were superheroes, Des Moines would be an unassuming Peter Parker. This mild Midwestern city\'s economy could be described as its hidden superpower. It\'s certainly one of Forbes magazine\'s darlings: described as a "Best Place for Business" in 2010, and as one of "America\'s Best Cities for Young Professionals" in 2011.<p>The city is also one of the world\'s insurance capitals, as the home of headquarters or major offices for Blue Cross Blue Shield, Nationwide Mutual Insurance Company, Principal Financial Group, Aviva, EMC Insurance Companies, and the Meredith Corporation.</p><p>When the city is off-duty it rocks just as hard. Although Forbes profiled the city\'s friendliness to young professionals, another demographic enjoys Des Moines just as much: families with children. If you\'re bringing kids with you to the Des Moines area, you\'ll be happy to known the area has held onto its Midwestern values, and has plenty to do if you\'re under 18. Start with the Blank Park Zoo, an extremely popular attraction known for its easy-to-navigate layout and its free camel rides. (Just remember what Aladdin told you about camels....) If your kids are more high-adrenaline than that, keep them enthralled with a visit to Adventureland, Des Moines\' favorite roller coaster park.</p><p>A number of museums keep Des Moines culturally and intellectually engaged. The best-known among locals is probably the Science Center of Iowa. A hit with all age groups, the live demonstrations, traveling exhibits (such as a current exhibit about Mars), and IMAX theater keep families, school groups, and adults coming back for more. The State Capitol is another point of interest, and a great place for kids to learn about government, democracy, and civic engagement.</p><p>If the adults need an evening out on the town, Des Moines has a lively theatre scene just waiting to be explored. Ballet Des Moines, Des Moines Metro Opera, the Ankeny Community Theatre and the Des Moines Community Theatre, and the Repertory Theatre of Iowa all light up their respective stages with acting, singing, and dancing talent.</p><p>Finally, Des Moines is also a shopper\'s delight. Find the bargains at Jordan Creek Town Center and Valley West Mall, or look for something more eclectic in the hip Historic East Village, a newly revitalized area filled with boutiques.</p><p>Beneath the polite Midwestern veneer is a hugely fun, dynamic city. Discover Des Moines, and let New Home Source help.</p> ]]></footer> </content>'; $subst = "$1 <p>$3</p><p>"; $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