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 Foundation let pattern = #"Seat (?P<playerseat>[0-9]+): (?P<playername>[.?]+)"# let regex = try! NSRegularExpression(pattern: pattern) let testString = ##""" he name of first value, first 17/04/2013 11:30:00 - A:30.0 PokerStars Hand #152844641611: Tournament #1556870024, $0.44+$0.44+$0.12 USD Hold'em No Limit - Level I (25/50) - 2016/05/04 13:15:07 BRT [2016/05/04 12:15:07 ET] Table '1556870024 3' 6-max Seat #1 is the button Seat 1: vitalijus (500 in chips) Seat 2: papoca555 (500 in chips) Seat 3: SITBACKNOW (500 in chips) Seat 4: hackerpex (500 in chips) Seat 5: vovan189 (500 in chips) Seat 6: DiMers80 (500 in chips) vitalijus: posts the ante 10 papoca555: posts the ante 10 SITBACKNOW: posts the ante 10 hackerpex: posts the ante 10 vovan189: posts the ante 10 DiMers80: posts the ante 10 papoca555: posts small blind 25 SITBACKNOW: posts big blind 50 *** HOLE CARDS *** Dealt to hackerpex [7h 6h] hackerpex: folds vovan189: calls 50 DiMers80: raises 440 to 490 and is all-in vitalijus: folds papoca555: folds SITBACKNOW: calls 440 and is all-in vovan189: calls 440 and is all-in *** FLOP *** [5c 2c 3s] *** TURN *** [5c 2c 3s] [Ah] *** RIVER *** [5c 2c 3s Ah] [4s] *** SHOW DOWN *** SITBACKNOW: shows [Ks Tc] (a straight, Ace to Five) vovan189: shows [6d As] (a straight, Deuce to Six) DiMers80: shows [Kc Th] (a straight, Ace to Five) vovan189 collected 1555 from pot vovan189 wins the $0.44 bounty for eliminating SITBACKNOW vovan189 wins the $0.44 bounty for eliminating DiMers80 SITBACKNOW finished the tournament in 35th place DiMers80 finished the tournament in 35th place *** SUMMARY *** Total pot 1555 | Rake 0 Board [5c 2c 3s Ah 4s] Seat 1: vitalijus (button) folded before Flop (didn't bet) Seat 2: papoca555 (small blind) folded before Flop Seat 3: SITBACKNOW (big blind) showed [Ks Tc] and lost with a straight, Ace to Five Seat 4: hackerpex folded before Flop (didn't bet) Seat 5: vovan189 showed [6d As] and won (1555) with a straight, Deuce to Six Seat 6: DiMers80 showed [Kc Th] and lost with a straight, Ace to Five PokerStars Hand #152844655854: Tournament #1556870024, $0.44+$0.44+$0.12 USD Hold'em No Limit - Level I (25/50) - 2016/05/04 13:15:28 BRT [2016/05/04 12:15:28 ET] Table '1556870024 3' 6-max Seat #2 is the button Seat 1: vitalijus (490 in chips) Seat 2: papoca555 (465 in chips) Seat 4: hackerpex (490 in chips) Seat 5: vovan189 (1555 in chips) vitalijus: posts the ante 10 papoca555: posts the ante 10 hackerpex: posts the ante 10 vovan189: posts the ante 10 hackerpex: posts small blind 25 vovan189: posts big blind 50 *** HOLE CARDS *** Dealt to hackerpex [Jd Ad] """## let stringRange = NSRange(location: 0, length: testString.utf16.count) let matches = regex.matches(in: testString, range: stringRange) var result: [[String]] = [] for match in matches { var groups: [String] = [] for rangeIndex in 1 ..< match.numberOfRanges { let nsRange = match.range(at: rangeIndex) guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue } let string = (testString as NSString).substring(with: nsRange) groups.append(string) } if !groups.isEmpty { result.append(groups) } } 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