Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • 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

/
/
gm

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 = "^(?<name1>[^>\\n]*?[a-z]) (?<year1>\\d{4})-(?<date1>\\d\\d-\\d\\d) ?(v\\d+)?(?<rest1>[^>\\n]*?(?= >|$))(?: > (?<name2>[^>\\n]*?[a-z]) (?<year2>\\d{4})-(?<date2>\\d\\d-\\d\\d) ?(v\\d+)?)?(?<rest2>[^>\\n]*)$"; final String string = "Using regex to identify two different sets of data with multiple parts\n\n" + "I have some file folders that I want to use reg expressions to \"cut up\" sections so I can reformat them. This is their general pattern:\n\n" + "* 2 Cold Scorpio 1998-04-13 v1 > Mick Foley 1997-09-22 v2 \\[Cactus Jack\\] - Whole Lotta Groove {Production}\n" + "* 2 Cold Scorpio 1998-11-08 v2 > JOB Squad 1998-11-08 v1 - Armed & Rambunctious {Production}\n" + "* 2 Cold Scorpio 1998-11-15 v3 > Al Snow 1998-10-17 v2 - Scurry v1.2 {Production}\n" + "* Acolytes, The 1998-11-21 v1 > Kurrgan 1997-12-08 v2 - Interrogation\n" + "* Acolytes, The 1999-01-02 v2 > Ministry Of Darkness, The 1999-02-13 - Follower\n" + "* Acolytes, The 1999-03-22 v3 > Undertaker, The 1995-11-19 v2 - Graveyard Symphony v3\n" + "* Acolytes, The 1999-10-18 v4 > Steve Williams 1999-03-21\n" + "* Acolytes, The 1999-10-31 v5 - T-Rex {Production}\n" + "* Adrian Adonis 1985-09-28 > Jimmy Hart 1985-03-31 - Eat Your Heart Out, Rick Springfield\n" + "* Adrian Adonis 1986-04-05 - You're So Vain {Mainstream}\n" + "* Aja Kong 1995-12-11 \\[Kwang\\] > Savio Vega 1994-01-30 v1 - Kwang Theme v1\n" + "* Akio 2003-11-20 v1 > Tajiri 2003-08-14 - Green Mist\n" + "* Al Snow 1996-02-24 v1 \\[Avatar\\] > Orient Express 1990-03-03 - Orient Express Theme\n" + "* Al Snow 1996-04-15 v2 \\[Leif Cassidy\\] > Rockers, The 1988-06-18 - Rockin Rockers – Rock Out v1\n" + "* Al Snow 1998-11-08 v3 > JOB Squad 1998-11-08 v1 - Armed & Rambunctious {Production}\n" + "* Al Snow 1999-11-04 v1 > Mick Foley 1999-01-25 v2 - Wreck v2\n" + "* Al Snow 2000-02-28 v1 > Head Cheese 2000-02-28 - Head Cheese\n\n" + "Before I was able to use the following expression to grab info when it was just a single portion:\n\n" + " (?<name>.*?[a-z]) (?<year>\\d{4})-(?<date>\\d\\d-\\d\\d( v\\d+)?) - (?<rest>.*)\n\n" + "However, the second set throws a monkey wrench in for those with >'s. I tried just duplicating the expression a second time like this:\n\n" + " (?<name>.*?[a-z]) (?<year>\\d{4})-(?<date>\\d\\d-\\d\\d) (v\\d+)? > (?<name>.*?[a-z]) (?<year>\\d{4})-(?<date>\\d\\d-\\d\\d) (v\\d+)? (?<rest>.*)\n\n" + "However, it's saying \"A subpattern name must be unique\". I have no idea how to fix this. Can anyone help?"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); 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