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

/
/
gm

Test String

Substitution

Processing...

Code Generator

Generated Code

use strict; my $str = 'EPISODE 148: The Flea Party TAFFY Per-fect! BENTLEY Gotcha! MRS M Aaaah! I must ask Forsythe not to leave his kitchen cabinets lying around on the floor like this! MRS M Oh my! I hope my little cherub doesn’t have fleas or I’ll have to take that ribbon right off and put a flea collar on you! TAFFY Take the ribbon off? TAFFY Fleas? Please! This is just a common itch, I don’t have fleas. BENTLEY Oh no? Well you will my friend, you will. And I know just where to find them FLEA 1 Cookie crumb? FLEA 2 Don’t mind if I do. MISH Moldy macaroon? MASH Thank you kindly! MISH & MASH One for me, one for flea. MASH Enjoy, little dudes! MISH Ouch! Hey, what was that?! MASH Ow! Keep yer paws off my hairdo! MISH Me!? You’re the one pocking my hair! MASH Are you kidding me? MISH Liar! MASH What did you say? MISH I said… MASH Moldy macaroon? MISH Don’t mind if I do. FLEA HEY LET US OUT!!! BENTLEY Yeah, yeah, be patient FLEA 1 Oh! Love that new host smell! You said it! Let’s settle here! BENTLEY Look! Fleas! Fleas!!! Whoo whoo whoo, fleas ! MRS M Oh dear! you’re not scratching again are you? BENTLEY Go ahead, scratch it up! You know you want to. MRS M Delightful dinero, a discothèque! MRS M Like summer nights in Salamanca. MRS M Don’t be a party pooper, Bentley, dance with your little brother! Flea Earthquake! Flea Let’s move from this place! MRS M Coming! TAFFY Huh. I don’t itch anymore. BENTLEY Of COURSE you do! You’re covered in flea-AA! MRS M Bentley, now you?! And it looks like you might need more than a flea collar, you may need a HOT FLEA BATH. MRS M Sorry dear, what was that again? TAFFY Ah! That will teach you to start a flea fight MRS M Well if you can’t decide, why not buy BOTH yachts, dear? MRS HIGHCOST Hello? Are you still there? MRS M Oh sorry darling. Can you get them in platinum? TAFFY I love scratching TAFFY Scratch scratch scratch scratch MRS HIGHCOST So… Platinum or diamond then? MRS M Oh sorry dear, let’s have a look in the magazine TAFFY Here buddy, this will help! BENTLEY Oh! Itchy wool! MRS M Well of course matching helicopters! What are we? Cave people? FORSYTHE I’ve nearly achieved...PERFECTION! FORSYTHE Out of my kitchen you fleabag! MRS M Animal control, I have a flea emergency! MASH There you are! We found you! TAFFY Found me? MASH Not you-– them! MISH & MASH Welcome home fellas! See ya! ANIMAL CONTROL This is worse than the Itchy Dingo Disaster of ‘05. TAFFY Showtime! MRS M Oh my hair! Oh! What a wonderful haircut for this summer! Thank you Taffy-Waffy '; my $regex = qr/^([A-Z][A-Z])/mp; my $subst = ' \\1'; my $result = $str =~ s/$regex/$subst/rg; print "The result of the substitution is' $result\n";

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