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

/
/
g

Test String

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 = "([0-9]{1,}:[0-9]{1,})"; final String string = "...New developments in the ongoing controversy \n" + "0:05 surrounding Molecular Genetics. \n" + "0:07 Incoming reports now allege \n" + "0:08 MolGen’s involvement in illegal human cloning. \n" + "0:11 In a startling development, it appears \n" + "0:12 private security forces have taken over. \n" + "0:15 All this just two weeks after the controversial disappearance \n" + "0:18 of MolGen Chief Medical Officer Dr. Nora Phillips. \n" + "0:22 Anonymous sources saying there could be actual human clones inside. \n" + "0:44 Up and at ‘em, Five. \n" + "0:45 Let’s go. Get up! \n" + "0:49 Sir, FBI just breached the lobby. \n" + "0:51 It’s getting down to the wire. \n" + "0:53 Guess we better hurry then. \n" + "0:55 Get up, Five! \n" + "0:58 Why do you always have to make everything so hard. \n" + "1:01 Let's go. \n" + "1:05 Chrissakes, is that really necessary? \n" + "1:08 Keep moving. Keep moving. \n" + "1:14 All clear, let's go. \n" + "1:22 Let’s go! Move, move, move! \n" + "1:24 Goddamn abomination. \n" + "1:32 In position. Driver’s coming in. \n" + "1:39 Abomination. \n" + "1:41 Get the door, please. \n" + "1:47 Abomination. \n" + "1:49 From the latin word \"abominari\". \n" + "1:52 A thing that causes hatred or disgust. \n" + "1:54 Gentlemen, the specimen stays with me at all times. \n" + "1:58 Lead, follow, you offer support. \n" + "1:59 There is also an air unit that is active. \n" + "2:02 Right now we are surrounded by the FBI. \n" + "2:05 We will move out. We will move fast. \n" + "2:07 And if necessary we will push back. \n" + "2:10 The only thing that matters now \n" + "2:12 is getting the merchandise to the customer. \n" + "2:18 Let’s see if you’re worth the money. \n" + "2:20 You don’t need to baby-sit me. \n" + "2:22 I baby-sit everyone. \n" + "2:24 That’s kinda my thing. \n" + "2:38 What the — \n" + "3:04 They’re moving in. You got six on your tail. \n" + "3:06 Xavier, do me a favor. \n" + "3:07 Push ‘em back a little bit. \n" + "3:24 This is your world? \n" + "3:27 Have you never seen it before? \n" + "3:28 Drive the car. \n" + "3:29 Not with my eyes, no. \n" + "3:30 Five. Shut up. \n" + "3:32 But I know all about your world. \n" + "3:33 Really? \n" + "3:34 Do not communicate with the specimen... \n" + "3:37 - Alaska has a longer coastline... - Five... \n" + "3:39 - ...than all other 49 states combined. - Stop talking. \n" + "3:40 - If you want, I could tell you some — - Five, stop talking! \n" + "3:44 My name is Lilly. \n" + "3:47 Okay. \n" + "3:50 Please, those are mine. \n" + "3:51 I love her work. It’s great, isn’t it. \n" + "3:54 Such detail. That’s my favorite. \n" + "3:57 She's really something else. \n" + "4:17 Okay... \n" + "4:19 Cut him off! \n" + "4:21 Stop the car. \n" + "4:26 Three seconds. \n" + "4:28 Two. \n" + "4:30 One. \n" + "4:35 Get out. \n" + "4:37 Okay. \n" + "4:38 Do you have any idea what you just did? \n" + "4:48 I might be a little rusty right now, \n" + "4:50 but I’ve been doing this for a long time. \n" + "4:52 I’m very good at it. \n" + "4:53 That only means your chances \n" + "4:54 of failure increase with each outing. \n" + "4:57 Statistically speaking. \n" + "4:59 The odds will be what the odds will be. \n" + "5:01 Get out. \n" + "5:03 You’re dead, mystery man. \n" + "5:05 You’re dead. \n" + "5:08 You mind? \n" + "5:18 Air unit one in pursuit. \n" + "5:20 No, no, no, no, the driver’s mine! \n" + "5:22 Buckle up... \n" + "5:36 Let’s go, let’s go! \n" + "5:47 Lower! Lower! \n" + "5:53 Lower! \n" + "5:54 What part of “lower” don’t you understand?! \n" + "7:58 Mercy. \n" + "7:59 From Old French, merced. \n" + "8:02 Kindness. Grace. Pity. \n" + "8:30 Put your hands up! \n" + "8:31 On your knees! \n" + "9:00 You okay? \n" + "9:05 Dr. Phillips. \n" + "9:08 Yes. \n" + "9:10 She made Tulip and Rose and Daffodil... \n" + "9:14 and me. \n" + "9:17 They’re all gone now. \n" + "9:22 Look. \n" + "9:39 Hello, Lilly. \n" + "9:56 Keep it. \n" + "9:57 Thank you.\n"; final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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