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 java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "^((.+)\\.TP.+\\.LS\\t)(.{2,99})$"; final String string = "BG_LOGIC.TP BG_LOGIC.LS BG Logic\n" + "CICLO.TP CICLO.LS Ciclo carico\n" + "CICLO_SIM.TP CICLO_SIM.LS Ciclo carico\n" + "CLSKP_G1.TP CLSKP_G1.LS set skip cond g1\n" + "CLSKP_G2.TP CLSKP_G2.LS set skip cond g2\n" + "CLSKP_G3.TP CLSKP_G3.LS set skip cond g3\n" + "CLSKP_G4.TP CLSKP_G4.LS set skip cond g4\n" + "CLSKP_G5.TP CLSKP_G5.LS set skip cond g5\n" + "CLSKP_G6.TP CLSKP_G6.LS set skip cond g6\n" + "CLSKP_G7.TP CLSKP_G7.LS set skip cond g7\n" + "CLSKP_G8.TP CLSKP_G8.LS set skip cond g8\n" + "CMD_TOOL.TP CMD_TOOL.LS Comandi tool\n" + "CTRL_COLORE.TP CTRL_COLORE.LS Controllo colore\n" + "CTRL_SMUS.TP CTRL_SMUS.LS Controllo smussi\n" + "DEFINREG.TP DEFINREG.LS Defin. registri\n" + "DEFINTOO.TP DEFINTOO.LS Definisci tool\n" + "DEF_PR.TP DEF_PR.LS Defin. PR ciclo\n" + "DEF_PR_DEMO.TP DEF_PR_DEMO.LS Defin. PR ciclo\n" + "DEMO_CLC.TP DEMO_CLC.LS Cycle DEMO\n" + "DEMO_OFF.TP DEMO_OFF.LS Ciclo Demo Offst\n" + "DOFF_NTV.TP DOFF_NTV.LS DemoOffs no Tav\n" + "DOFF_TAV.TP DOFF_TAV.LS DemoOffs con Tav\n" + "GETDATA.TP GETDATA.LS Get PC Data\n" + "HOME.TP HOME.LS Vai a Home\n" + "HOMESCAR.TP HOMESCAR.LS Home+Scarto\n" + "INIT.TP INIT.LS InitVars\n" + "INIT_CICLO.TP INIT_CICLO.LS InitCiclo\n" + "INIT_POS.TP INIT_POS.LS InitPosizioni\n" + "LASCIA.TP LASCIA.LS Lascia pastiglia\n" + "MAGNETE1.TP MAGNETE1.LS Cmd magnete A\n" + "MAGNETE2.TP MAGNETE2.LS Cmd magnete B\n" + "MANUT.TP MANUT.LS Manutenzione\n" + "OFFSET.TP OFFSET.LS Offset da PLC\n" + "OVERRIDE.TP OVERRIDE.LS Assegna Override\n" + "OVER_BAS.TP OVER_BAS.LS Override ridotta\n" + "OVER_LIM.TP OVER_LIM.LS BG_Logic\n" + "OVER_RIP.TP OVER_RIP.LS BG_Logic\n" + "POSIZIONE.TP POSIZIONE.LS COMPARATORE\n" + "PREDISP_TAV.TP PREDISP_TAV.LS Piano formella\n" + "PRENDI.TP PRENDI.LS Prendi pastiglia\n" + "PRG_1.TP PRG_1.LS 1336\n" + "PRG_2.TP PRG_2.LS 549 Hole\n" + "PRG_3.TP PRG_3.LS 201659.100\n" + "PRG_998.TP PRG_998.LS NoHole\n" + "PRG_999.TP PRG_999.LS Hole\n" + "PRIOR_PREL.TP PRIOR_PREL.LS Prior. prelievo\n" + "PROVA.TP PROVA.LS Test\n" + "PULPZ.TP PULPZ.LS Pulizia pinza\n" + "REQMENU.TP REQMENU.LS Request PC Menu\n" + "RISCAL.TP RISCAL.LS Riscaldamento\n" + "RSR0001.TP RSR0001.LS Prog. Avvio\n" + "SCARTO.TP SCARTO.LS Scarto\n" + "SENDDATA.TP SENDDATA.LS Send PC Data\n" + "SENDEVNT.TP SENDEVNT.LS Send PC Event\n" + "SENDSYSV.TP SENDSYSV.LS Send PC SysVar\n" + "SETSKCOL.TP SETSKCOL.LS setskipcondition\n" + "SET_SR.TP SET_SR.LS Set string reg.\n" + "TARA_COLORE.TP TARA_COLORE.LS Tara ctrl colore\n" + "TEST.TP TEST.LS Temp\n" + "TEST_MOV.TP TEST_MOV.LS Prova movimenti\n" + "VRFYPREL.TP VRFYPREL.LS Check Prel. Nas.\n" + "VRFYPRTV.TP VRFYPRTV.LS Check Prel. Tav.\n" + "_MAIN.TP _MAIN.LS Prog. principale\n" + "_RESET.TP _RESET.LS Carica punti\n"; final String subst = "PRG\\t\\2\\t\\3"; 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