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
No Match

r"
"
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 = "[^\\d\\.](((01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)[\\. ]?[\\dX]{3}[\\. ]?[\\dX]{2}[\\. \\-]?[\\dX]{2}|(01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)[\\. ]?[\\dX]{3}[\\. ]?[\\dX]{2}|(01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)[\\. ]?[\\dX]{3})(?![\\d\\.X]))|([^\\d\\.](01|03|07|11|13|17|19|21|23|25|27|29|31|33|35|37|39|43|45|47|49|53|55|59|61|65|67|71|73|75|77|79|81|83|85|87|91|93|95|97|99)(?![\\d\\.X]|( ?%)))"; final String string = " HS codes: 271012 (Petroleum oils and oils obtained from bituminous minerals, other than crude; preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the preparations; 33.234-33.237 waste oils : Petroleum oils and oils obtained from bituminous minerals (other than crude) and preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the preparations, other than those containinandandandg biodiesel and other than waste oils : Light oils and preparations), 271019 (Petroleum oils and oils obtained from bituminous minerals, other than crude; preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the \n" + "preparations; waste oils : Petroleum oils and oils obtained from bituminous minerals (other than crude) and preparations not elsewhere specified or included, containing by weight 70 % or more of petroleum oils or of oils obtained from bituminous minerals, these oils being the basic constituents of the preparations, other than those containing biodiesel and other than waste oils : Other);\n" + "ICS codes: 03.120 (Quality), 13.020 (Environmental protection), 75.160 (Fuels);HS 1234 2134 5325 ösadlkjföasfas \n" + "HS: 8418 694X\n" + "HS: 8415 10XX\n" + "HS: 8418 694X\n" + "HS: 8415 10XX\n" + "HS 8418 101X, 8418 21XX\n" + "ICS codes: 23.120 (Ventilators. Fans. Air-conditioners), 27.200 (Refrigerating technology), 71.100.45 (Refrigerants and antifreezes);ICS codes: 23.120 (Ventilators. Fans. Air-conditioners), 27.200 (Refrigerating technology), 71.100.45 (Refrigerants and antifreezes); \n" + "ICS codes: 43.040.65 (Glazing and wiper systems)\n" + "HS codes:\n" + "39.26.90.90.90\n" + "70.07.11.00.00\n" + "70.07.21.00.00\n" + "234\n" + "87.08.29.50.00\n" + "ICS-Codes:\n" + "83.160 (Tyres)\n" + "(HS 4011).\n" + "Electrical and electronic equipment (EEE). More detail on product coverage can be found in Restriction of the Use of Certain Hazardous Substances in Electrical and Electro 23XX n ic Equipment Regulations 2012 (SI 2012/3032) (https://www.legislation.gov.uk/uksi/2012/3032/made). HS code 9018 for medical devices. Electronic Components in general (ICS 31.020); Electronic components in general (ICS 31.020)\n" + "Air emission sources; Quality (ICS code(s): 03.120); Environmental protection (ICS code(s): 13.020); Air quality (ICS code(s): 13.040); Test conditions and procedures in general (ICS code(s): 19.020)\n" + "Passenger car (including New Energy Vehicle) (HS code(s): 8703); (ICS code(s): 43.060; 43.080.01; 43.100; 75.160)\n" + "ICS codes: 29, 33, 97, 29 , 33, 97;\n\n" + "75.16X\n\n" + "13\n" + "13.123.34-34"; 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