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

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