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

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

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 Python, please visit: https://docs.python.org/3/library/re.html