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

/
/
gm

Test String

Substitution

Processing...

Code Generator

Generated Code

import Foundation let pattern = #"^(.+)(\[ *)(\d+)(\] *)(.+ )(.+)$"# let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines) let testString = ##""" DIN[ 1] ON DCS_SpeedSafe DIN[ 201] ON *IMSTP DIN[ 202] ON *HOLD DIN[ 203] ON SFSPD DIN[ 204] OFF CSTOPI DIN[ 205] OFF FAULT RESET DIN[ 206] OFF START DIN[ 207] OFF HOME DIN[ 208] ON ENBL DIN[ 209] OFF RSR1 DIN[ 210] OFF RSR2 DIN[ 211] OFF RSR3 DIN[ 212] OFF RSR4 DIN[ 213] OFF RSR5 DIN[ 214] OFF RSR6 DIN[ 215] OFF RSR7 DIN[ 216] OFF RSR8 DIN[ 219] OFF SempreOff DIN[ 220] OFF Abilita Ciclo DIN[ 221] OFF Abilitazione generale DIN[ 225] OFF CaricoOk DIN[ 226] OFF CarFuoriIngombro DIN[ 227] OFF TraslOK DIN[ 228] OFF TraslFuoriIngombro DIN[ 229] OFF PezPresNastroCarico DIN[ 230] OFF FtControlloSagoma1 DIN[ 231] OFF FtControlloSagoma2 DIN[ 232] OFF TipSagoma1 DIN[ 233] OFF TipSagoma2 DIN[ 234] OFF TavRqDepo1 DIN[ 235] OFF TavRqScambio1 DIN[ 236] OFF TavRqPrel1 DIN[ 237] OFF TavPezScarto DIN[ 238] OFF TavFuoriImgombro DIN[ 239] OFF TavInclusa DIN[ 240] OFF PrelievoNastroCarico DIN[ 241] OFF PrelievoTavola DIN[ 242] OFF ScartoCtrlUsura DIN[ 243] OFF TavPezPres1 DIN[ 244] OFF TavPezPres2 DIN[ 245] OFF Ctrl Ft Enable DIN[ 246] OFF TavRqDepo2 DIN[ 247] OFF TavRqScambio2 DIN[ 248] OFF TavRqPrel2 DIN[ 249] OFF NastroAbil DIN[ 250] OFF Carico2Ok DIN[ 251] OFF Car2FuoriIng DIN[ 252] OFF PezPresNastro2 DIN[ 253] OFF Nastro2Abil DIN[ 254] OFF PriorAltern/Nas2 DIN[ 255] OFF DemoOffsEsegui DIN[ 256] OFF DemoOffsAddOffs DIN[ 257] OFF EnbCtrlSmussi DIN[ 258] OFF CtrlSmussiRisKo DIN[ 259] OFF CtrlSmussiRisOk DIN[ 260] OFF EnbCtrlColore DIN[ 261] OFF CtrlColoreRisKo DIN[ 262] OFF CtrlColoreRisOk DIN[ 263] OFF CassScartoPres DIN[ 265] OFF Prel1_StopRq DIN[ 266] OFF Prel2_StopRq DIN[ 267] OFF DepoTrasl_StopRq DIN[ 268] OFF DepoTav1_StopRq DIN[ 269] OFF DepoTav2_StopRq DIN[ 270] OFF CtrlSmus_StopRq DIN[ 273] OFF Magnet_A DIN[ 274] OFF Smagnet_A DIN[ 275] OFF Magnet_B DIN[ 276] OFF Smagnet_B DIN[ 277] OFF SoffioA DIN[ 278] OFF SoffioB DOUT[ 201] OFF CMDENBL DOUT[ 202] ON SYSRDY DOUT[ 203] OFF PROGRUN DOUT[ 204] OFF PAUSED DOUT[ 205] OFF HELD DOUT[ 206] OFF FAULT DOUT[ 207] ON ATPERCH DOUT[ 208] OFF TPENBL DOUT[ 209] OFF BATALM DOUT[ 210] OFF BUSY DOUT[ 211] OFF ACK1 DOUT[ 212] OFF ACK2 DOUT[ 213] OFF ACK3 DOUT[ 214] OFF ACK4 DOUT[ 215] OFF ACK5 DOUT[ 216] OFF ACK6 DOUT[ 217] OFF NumProgErr DOUT[ 218] ON InHome DOUT[ 219] OFF InManut DOUT[ 220] OFF InRiscaldamento DOUT[ 221] OFF InPulizPz DOUT[ 222] OFF InCheckFt DOUT[ 223] OFF SegnaliSimulati DOUT[ 224] OFF Modo T1 Attivo DOUT[ 233] OFF CicloRiposo(Tappa0) DOUT[ 234] OFF PezPrelNastroCarico DOUT[ 235] OFF CicloNastroCarico DOUT[ 236] ON NastroCaricoFuoriIng DOUT[ 237] OFF PezDepTrasl DOUT[ 238] OFF CicloTrasl DOUT[ 239] ON TraslFuoriIng DOUT[ 240] OFF PezScarto DOUT[ 241] OFF PzNoPrelNastroCarico DOUT[ 242] OFF TavPezDep1 DOUT[ 243] OFF TavPezScambio1 DOUT[ 244] OFF TavPezPrel1 DOUT[ 245] ON TavFuoriIng DOUT[ 246] OFF CicloTav DOUT[ 247] OFF TavPezNoPrel1 DOUT[ 248] OFF TavPezNoPrel2 DOUT[ 249] OFF TavPezDep2 DOUT[ 250] OFF TavPezScambio2 DOUT[ 251] OFF TavPezPrel2 DOUT[ 252] OFF PezPrelNastro2 DOUT[ 253] OFF CicloNastro2 DOUT[ 254] OFF FuoriIngNastro2 DOUT[ 255] OFF PezNoPrelNas2 DOUT[ 256] OFF DemoOfsPosOk DOUT[ 257] OFF PosCtrlSmussi DOUT[ 258] OFF PosCtrlColore DOUT[ 259] ON FuoriIngCtrlSmus DOUT[ 260] ON FuoriIngProfil DOUT[ 267] OFF MagneteAAttira DOUT[ 268] OFF MagnateARilascia DOUT[ 269] OFF MagneteBAttira DOUT[ 270] OFF MagneteBRilascia DOUT[ 273] OFF Prel1_Stop DOUT[ 274] OFF Prel2_Stop DOUT[ 275] OFF DepoTrasl_Stop DOUT[ 276] OFF DepoTav1_Stop DOUT[ 277] OFF DepoTav2_Stop DOUT[ 278] OFF CtrlSmus_Stop GIN[ 1] 0 NumProg GIN[ 2] 0 VelCiclo GIN[ 3] 0 CodPastiglia GIN[ 4] 0 Offset X GIN[ 5] 0 Offset Y GIN[ 6] 0 Offset Z GIN[ 7] 0 Offset rotaz X GIN[ 8] 0 Offset rotaz Y GIN[ 9] 0 Offset rotaz Z GIN[ 15] 0 CmdMan GOUT[ 1] 0 NumeroProgAttivo GOUT[ 2] 0 ComandoEseguito GOUT[ 3] 0 DemoOffsNumPos UI[ 1] ON *IMSTP UI[ 2] ON *Hold UI[ 3] ON *SFSPD UI[ 4] OFF Cycle stop UI[ 5] OFF Fault reset UI[ 6] OFF Start UI[ 7] OFF Home UI[ 8] ON Enable UI[ 9] OFF Rsr0001 UI[ 10] OFF RSR2/PNS2/STYLE2 UI[ 11] OFF RSR3/PNS3/STYLE3 UI[ 12] OFF RSR4/PNS4/STYLE4 UI[ 13] OFF RSR5/PNS5/STYLE5 UI[ 14] OFF RSR6/PNS6/STYLE6 UI[ 15] OFF RSR7/PNS7/STYLE7 UI[ 16] OFF RSR8/PNS8/STYLE8 UO[ 1] OFF Cmd enabled UO[ 2] ON System ready UO[ 3] OFF Prg running UO[ 4] OFF Prg paused UO[ 5] OFF Motion held UO[ 6] OFF Fault UO[ 7] ON At perch UO[ 8] OFF TP enabled UO[ 9] OFF Batt alarm UO[ 10] OFF Busy UO[ 11] OFF ACK1/SNO1 UO[ 12] OFF ACK2/SNO2 UO[ 13] OFF ACK3/SNO3 UO[ 14] OFF ACK4/SNO4 UO[ 15] OFF ACK5/SNO5 UO[ 16] OFF ACK6/SNO6 SI[ 1] OFF Fault reset SI[ 2] ON Remote SI[ 3] ON Hold SI[ 4] OFF User PB#1 SI[ 5] OFF User PB#2 SI[ 6] OFF Cycle start SI[ 8] ON CE/CR Select b0 SI[ 9] ON CE/CR Select b1 SO[ 1] OFF Cycle start SO[ 2] OFF Hold SO[ 3] OFF Fault LED SO[ 4] OFF Batt alarm SO[ 5] OFF User LED#1 SO[ 6] OFF User LED#2 SO[ 7] OFF TP enabled RO[ 1] OFF SoffioA RO[ 2] OFF SoffioB FLG[ 1] OFF DCS_SpeedSafeMem FLG[ 2] OFF Simula FLG[ 3] OFF RemoteOverDsb """## let stringRange = NSRange(location: 0, length: testString.utf16.count) let substitutionString = #"\1 \t \3 \t \6"# 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