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 = "(--[ \\S]*\\n)"; final String string = "POROSITY-TOTAL POROSITY-TOTAL -- Generated : Petrel\n" + "-- Property name in Petrel : PHIT_Edit_July2016\n" + " 0.1085 0.1136 0.1124 0.1142 0.1141 0.1114 0.1118 0.1158 0.1147 0.1109 0.1216 0.1193 0.1179 0.1204 0.1201 0.1181 0.1200 0.1133\n" + " 0.1085 0.1038 0.1061 0.1047 0.1006 0.1013 0.1004 0.0976 0.0992 0.1017 0.1034 0.0954 0.0947 0.0964 0.0965 0.0932 0.0902 0.0963\n" + " 0.0993 0.0919 0.0833 0.0934 0.0916 0.1010 0.1024 0.0971 0.0927 0.0949 0.0956 0.0921 0.0879 0.0954 0.0892 0.0935 0.0895 0.0834\n" + " 0.0905 0.0924 0.0857 0.0868 0.0823 0.0751 0.0758 0.0765 0.0681 0.0686 0.0714 0.0728 0.0711 0.0760 0.0770 0.0696 0.0672 0.0685\n" + " 0.0730 0.0684 0.0641 0.0659 0.0714 0.0697 0.0728 0.0725 0.0685 0.0642 0.0600 0.0641 0.0759 0.0665 0.0648 0.0714 0.0742 0.0750\n" + " 0.0701 0.0725 0.0721 0.0669 0.0639 0.0669 0.0674 0.0686 0.0657 0.0666 0.0558 0.0486 0.0478 0.0452 0.0463 0.0498 0.0444 0.0392\n" + " 0.0356 0.0424 0.0405 0.0367 0.0274 0.0263 0.0281 0.0252 0.0252 0.0315 0.0322 0.0284 0.0307 0.0255 0.0237 0.0246 0.0258 0.0306\n" + " 0.0259 0.0230 0.0176 0.0185 0.0205 0.0192 0.0191 0.0204 0.0193 0.0276 0.0220 0.0231 0.0209 0.0293 0.0250 0.0227 0.0286 0.0346"; 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