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

Substitution

Processing...

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 = "(^\\w{3} \\d{1,2} [0-9:]{8}) (\\b\\w+[.]\\w*\\b) +(\\b\\w*\\b): (\\b\\w+\\b) ([\\w' ]*).*\\(([\\w.]*)\\)$"; final String string = "Jan 31 00:09:39 ubuntu.local ticky: INFO Created ticket [#4217] (mdouglas)\n" + "Jan 31 00:16:25 ubuntu.local ticky: INFO Closed ticket [#1754] (noel)\n" + "Jan 31 00:21:30 ubuntu.local ticky: ERROR The ticket was modified while updating (breee)\n" + "Jan 31 00:44:34 ubuntu.local ticky: ERROR Permission denied while closing ticket (ac)\n" + "Jan 31 01:00:50 ubuntu.local ticky: INFO Commented on ticket [#4709] (blossom)\n" + "Jan 31 01:29:16 ubuntu.local ticky: INFO Commented on ticket [#6518] (rr.robinson)\n" + "Jan 31 01:33:12 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mcintosh)\n" + "Jan 31 01:43:10 ubuntu.local ticky: ERROR Tried to add information to closed ticket (jackowens)\n" + "Jan 31 01:49:29 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mdouglas)\n" + "Jan 31 02:30:04 ubuntu.local ticky: ERROR Timeout while retrieving information (oren)\n" + "Jan 31 02:55:31 ubuntu.local ticky: ERROR Ticket doesn't exist (xlg)\n" + "Jan 31 03:05:35 ubuntu.local ticky: ERROR Timeout while retrieving information (ahmed.miller)\n" + "Jan 31 03:08:55 ubuntu.local ticky: ERROR Ticket doesn't exist (blossom)\n" + "Jan 31 03:39:27 ubuntu.local ticky: ERROR The ticket was modified while updating (bpacheco)\n" + "Jan 31 03:47:24 ubuntu.local ticky: ERROR Ticket doesn't exist (enim.non)\n" + "Jan 31 04:30:04 ubuntu.local ticky: ERROR Permission denied while closing ticket (rr.robinson)\n" + "Jan 31 04:31:49 ubuntu.local ticky: ERROR Tried to add information to closed ticket (oren)\n" + "Jan 31 04:32:49 ubuntu.local ticky: ERROR Timeout while retrieving information (mcintosh)\n" + "Jan 31 04:44:23 ubuntu.local ticky: ERROR Timeout while retrieving information (ahmed.miller)\n" + "Jan 31 04:44:46 ubuntu.local ticky: ERROR Connection to DB failed (jackowens)\n" + "Jan 31 04:49:28 ubuntu.local ticky: ERROR Permission denied while closing ticket (flavia)\n" + "Jan 31 05:12:39 ubuntu.local ticky: ERROR Tried to add information to closed ticket (oren)\n" + "Jan 31 05:18:45 ubuntu.local ticky: ERROR Tried to add information to closed ticket (sri)\n" + "Jan 31 05:23:14 ubuntu.local ticky: INFO Commented on ticket [#1097] (breee)\n" + "Jan 31 05:35:00 ubuntu.local ticky: ERROR Connection to DB failed (nonummy)\n" + "Jan 31 05:45:30 ubuntu.local ticky: INFO Created ticket [#7115] (noel)\n" + "Jan 31 05:51:30 ubuntu.local ticky: ERROR The ticket was modified while updating (flavia)\n" + "Jan 31 05:57:46 ubuntu.local ticky: INFO Commented on ticket [#2253] (nonummy)\n" + "Jan 31 06:12:02 ubuntu.local ticky: ERROR Connection to DB failed (oren)"; final String subst = " \\1 \\2"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); // The substituted value will be contained in the result variable final String result = matcher.replaceAll(subst); System.out.println("Substitution result: " + result); } }

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