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 Foundation let pattern = #"(^\w{3} \d{1,2} [0-9:]{8}) (\b\w+[.]\w*\b) +(\b\w*\b): (\b\w+\b) ([\w' ]*).*\(([\w.]*)\)$"# let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines) let testString = ##""" Jan 31 00:09:39 ubuntu.local ticky: INFO Created ticket [#4217] (mdouglas) Jan 31 00:16:25 ubuntu.local ticky: INFO Closed ticket [#1754] (noel) Jan 31 00:21:30 ubuntu.local ticky: ERROR The ticket was modified while updating (breee) Jan 31 00:44:34 ubuntu.local ticky: ERROR Permission denied while closing ticket (ac) Jan 31 01:00:50 ubuntu.local ticky: INFO Commented on ticket [#4709] (blossom) Jan 31 01:29:16 ubuntu.local ticky: INFO Commented on ticket [#6518] (rr.robinson) Jan 31 01:33:12 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mcintosh) Jan 31 01:43:10 ubuntu.local ticky: ERROR Tried to add information to closed ticket (jackowens) Jan 31 01:49:29 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mdouglas) Jan 31 02:30:04 ubuntu.local ticky: ERROR Timeout while retrieving information (oren) Jan 31 02:55:31 ubuntu.local ticky: ERROR Ticket doesn't exist (xlg) Jan 31 03:05:35 ubuntu.local ticky: ERROR Timeout while retrieving information (ahmed.miller) Jan 31 03:08:55 ubuntu.local ticky: ERROR Ticket doesn't exist (blossom) Jan 31 03:39:27 ubuntu.local ticky: ERROR The ticket was modified while updating (bpacheco) Jan 31 03:47:24 ubuntu.local ticky: ERROR Ticket doesn't exist (enim.non) Jan 31 04:30:04 ubuntu.local ticky: ERROR Permission denied while closing ticket (rr.robinson) Jan 31 04:31:49 ubuntu.local ticky: ERROR Tried to add information to closed ticket (oren) Jan 31 04:32:49 ubuntu.local ticky: ERROR Timeout while retrieving information (mcintosh) Jan 31 04:44:23 ubuntu.local ticky: ERROR Timeout while retrieving information (ahmed.miller) Jan 31 04:44:46 ubuntu.local ticky: ERROR Connection to DB failed (jackowens) Jan 31 04:49:28 ubuntu.local ticky: ERROR Permission denied while closing ticket (flavia) Jan 31 05:12:39 ubuntu.local ticky: ERROR Tried to add information to closed ticket (oren) Jan 31 05:18:45 ubuntu.local ticky: ERROR Tried to add information to closed ticket (sri) Jan 31 05:23:14 ubuntu.local ticky: INFO Commented on ticket [#1097] (breee) Jan 31 05:35:00 ubuntu.local ticky: ERROR Connection to DB failed (nonummy) Jan 31 05:45:30 ubuntu.local ticky: INFO Created ticket [#7115] (noel) Jan 31 05:51:30 ubuntu.local ticky: ERROR The ticket was modified while updating (flavia) Jan 31 05:57:46 ubuntu.local ticky: INFO Commented on ticket [#2253] (nonummy) Jan 31 06:12:02 ubuntu.local ticky: ERROR Connection to DB failed (oren) """## let stringRange = NSRange(location: 0, length: testString.utf16.count) let substitutionString = #" \1 \2"# let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString) print(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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression