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

~
~
xg

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 = "\\#(?P<cycle>\\d+)\\s\n" + " (?P<date>[\\d/]+)\\s\n" + " (?P<time>[\\d:]+)\\s+\n" + " Total:\\s(?P<total>[\\d.]+)[-\\s]+\n" + " RCPE:\\s+(?P<rcpe>\\d+)\\s\n" + " ID:\\s(?P<id>\\d+)\\s\n" + " WKOD:\\s+(?P<wkod>\\d+)\\s\n" + " OPRT:\\s+(?P<oprt>\\d+)\\s\n" + " TARE:\\s(?P<tare>[.\\d]+)"; final String string = "--------- #1157 11/06/2015 09:44:21 Total: 2482.3 ---------\n" + "RCPE: 101 ID: 204 WKOD: 0 OPRT: 0 TARE: 13.6\n" + "MAT ADDI(2) REGR(4) ADDI(5) ADDI(6) NATU(8)\n" + " 2% 25% 0.5% 1.3% 100 \n" + "FINA R 1.89 25.36 0.54 1.31 100.00\n" + "FINA W 33.7 629.4 9.6 23.3 1786.1\n" + "1st DW 22.8 629.4 9.6 23.3 1786.1\n" + "1st DT 79.0 1578.0 3622.0 9753.0 8468.0\n" + "1st FR 449.37 396.19 2.47 2.38 212.82\n" + " DW/DT 288.40 398.88 2.66 2.39 210.93\n" + "FRate 449.37 396.19 2.57 2.38 211.87\n" + "Retry# 02 \n\n" + "--------- #1158 11/06/2015 09:45:40 Total: 2513.7 ---------\n" + "RCPE: 101 ID: 204 WKOD: 0 OPRT: 0 TARE: 12.4\n" + "MAT ADDI(2) REGR(4) ADDI(5) ADDI(6) NATU(8)\n" + " 2% 25% 0.5% 1.3% 100 \n" + "FINA R 1.81 25.48 0.49 1.28 100.00\n" + "FINA W 32.8 640.4 8.8 23.2 1808.4\n" + "1st DW 21.1 640.4 8.8 23.2 1705.8\n" + "1st DT 80.0 1578.0 3524.0 9875.0 8456.0\n" + "1st FR 449.37 396.19 2.57 2.38 211.87\n" + " DW/DT 263.20 405.85 2.51 2.35 201.73\n" + "FRate 449.37 396.19 2.57 2.38 206.80\n" + "Retry# 01 01 \n\n" + "--------- #1159 11/06/2015 09:46:43 Total: 2484.9 ---------\n" + "RCPE: 101 ID: 204 WKOD: 0 OPRT: 0 TARE: 12.3\n" + "MAT ADDI(2) REGR(4) ADDI(5) ADDI(6) NATU(8)\n" + " 2% 25% 0.5% 1.3% 100 \n" + "FINA R 1.83 25.36 0.51 1.26 100.00\n" + "FINA W 32.8 630.2 9.1 22.6 1790.2\n" + "1st DW 24.3 630.2 9.1 22.6 1790.2\n" + "1st DT 80.0 1578.0 3489.0 9775.0 8710.0\n" + "1st FR 449.37 396.19 2.57 2.38 206.80\n" + " DW/DT 303.24 399.39 2.60 2.31 205.53\n" + "FRate 449.37 396.19 2.57 2.38 206.80\n" + "Retry# 01 \n\n" + "--------- #1160 11/06/2015 09:47:58 Total: 2581.8 ---------\n" + "RCPE: 101 ID: 204 WKOD: 0 OPRT: 0 TARE: 12.7\n" + "MAT ADDI(2) REGR(4) ADDI(5) ADDI(6) NATU(8)\n" + " 2% 25% 0.5% 1.3% 100 \n" + "FINA R 1.91 25.06 0.49 1.30 100.00\n" + "FINA W 35.6 646.9 9.1 24.3 1865.9\n" + "1st DW 23.8 646.9 7.5 24.3 1865.9\n" + "1st DT 83.0 1578.0 3636.0 10188.0 8633.0\n" + "1st FR 449.37 396.19 2.57 2.38 206.80\n" + " DW/DT 287.02 409.98 2.07 2.38 216.13\n" + "FRate 449.37 396.19 2.32 2.38 211.47\n" + "Retry# 02 01 \n\n" + "--------- #1161 11/06/2015 09:49:01 Total: 2645.1 ---------\n" + "RCPE: 101 ID: 204 WKOD: 0 OPRT: 0 TARE: 12.3\n" + "MAT ADDI(2) REGR(4) ADDI(5) ADDI(6) NATU(8)\n" + " 2% 25% 0.5% 1.3% 100 \n" + "FINA R 1.87 24.36 0.52 1.34 100.00\n" + "FINA W 36.1 644.3 10.1 25.9 1928.8\n" + "1st DW 24.8 644.3 10.1 25.9 1928.8\n" + "1st DT 86.0 1578.0 4159.0 10532.0 8454.0\n" + "1st FR 449.37 396.19 2.32 2.38 211.47\n" + " DW/DT 288.18 408.28 2.43 2.46 228.15\n" + "FRate 449.37 396.19 2.32 2.42 219.81\n" + "Retry# 02 "; final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS); 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