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

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 = "YYYYMMDD HHMISS beginning date of simulation"; final String string = "=====================FORMER PATHNAMES FILE===================\n" + "/home/sun_mod/forecast/flexwrf_foshan/workdir/2022-10-30_12:00:00/1373A/\n" + "/home/sun_mod/forecast/flexwrf_foshan/workdir/2022-10-30_12:00:00/\n" + "/home/sun_mod/forecast/flexwrf_foshan/workdir/2022-10-30_12:00:00/AVAILABLE.foshan\n" + "=============================================================\n" + "=====================FORMER COMMAND FILE=====================\n" + " -1 LDIRECT: 1 for forward simulation, -1 for backward simulation\n" + " 20221030 160000 YYYYMMDD HHMISS beginning date of simulation\n" + " 20221031 160000 YYYYMMDD HHMISS ending date of simulation\n" + " 3600 SSSSS (int) output every SSSSS seconds\n" + " 3600 SSSSS (int) time average of output (in SSSSS seconds)\n" + " 180 SSSSS (int) sampling rate of output (in SSSSS seconds)\n" + " 999999999 SSSSS (int) time constant for particle splitting (in seconds)\n" + " 180 SSSSS (int) synchronisation interval of flexpart (in seconds)\n" + " 10. CTL (real) factor by which time step must be smaller than tl\n" + " 10 IFINE (int) decrease of time step for vertical motion by factor ifine\n" + " 5 IOUT 1 concentration, 2 mixing ratio, 3 both, 4 plume traject, 5=1+4\n" + " 0 IPOUT particle dump: 0 no, 1 every output interval, 2 only at end\n" + " 0 LSUBGRID subgrid terrain effect parameterization: 1 yes, 0 no\n" + " 0 LCONVECTION convection: 3 yes, 0 no\n" + " 3600. DT_CONV (real) time interval to call convection, seconds\n" + " 0 LAGESPECTRA age spectra: 1 yes, 0 no\n" + " 0 IPIN continue simulation with dumped particle data: 1 yes, 0 no\n" + " 0 IFLUX calculate fluxes: 1 yes, 0 no\n" + " 1 IOUTPUTFOREACHREL CREATE AN OUPUT FILE FOR EACH RELEASE LOCATION: 1 YES, 0 NO\n" + " 0 MDOMAINFILL domain-filling trajectory option: 1 yes, 0 no, 2 strat. o3 tracer\n" + " 1 IND_SOURCE 1=mass unit , 2=mass mixing ratio unit\n" + " 1 IND_RECEPTOR 1=mass unit , 2=mass mixing ratio unit\n" + " 0 NESTED_OUTPUT shall nested output be used? 1 yes, 0 no\n" + " 0 LINIT_COND INITIAL COND. FOR BW RUNS: 0=NO,1=MASS UNIT,2=MASS MIXING RATIO UNIT\n" + " 1 TURB_OPTION 0=no turbulence; 1=diagnosed as in flexpart_ecmwf; 2 and 3=from tke.\n" + " 1 CBL SCHEME 0=no, 1=yes. works if TURB_OPTION=1\n" + " 1 SFC_OPTION 0=default computation of u*, hflux, pblh, 1=from wrf\n" + " -1 WIND_OPTION 0=snapshot winds, 1=mean winds,2=snapshot eta-dot,-1=w based on divergence\n" + " 0 TIME_OPTION 1=correction of time validity for time-average wind, 0=no need\n" + " 0 OUTGRID_COORD 0=wrf grid(meters), 1=regular lat/lon grid\n" + " 1 RELEASE_COORD 0=wrf grid(meters), 1=regular lat/lon grid\n" + " 2 IOUTTYPE 0=default binary, 1=ascii (for particle dump only),2=netcdf\n" + " 999 NCTIMEREC (int) Time frames per output file, only used for netcdf\n" + " 0 VERBOSE VERBOSE MODE,0=minimum, 100=maximum\n" + "=====================FORMER AGECLASESS FILE==================\n" + " 2 NAGECLASS number of age classes\n" + " 7200 SSSSSS (int) age class in SSSSS seconds\n" + " 999999 SSSSSS (int) age class in SSSSS seconds\n" + "=====================FORMER OUTGRID FILE=====================\n" + " 111.0 OUTLONLEFT geograhical longitude of lower left corner of output grid\n" + " 21.0 OUTLATLOWER geographical latitude of lower left corner of output grid\n" + " 834 NUMXGRID number of grid points in x direction (= # of cells )\n" + " 600 NUMYGRID number of grid points in y direction (= # of cells )\n" + " 0 OUTGRIDDEF outgrid defined 0=using grid distance, 1=upperright corner coordinate\n" + " 1000.0 DXOUTLON grid distance in x direction or upper right corner of output grid\n" + " 1000.0 DYOUTLON grid distance in y direction or upper right corner of output grid\n" + " 5 NUMZGRID number of vertical levels\n" + " 5.0 LEVEL height of level (upper boundary)\n" + " 10.0 LEVEL height of level (upper boundary)\n" + " 100.0 LEVEL height of level (upper boundary)\n" + " 500.0 LEVEL height of level (upper boundary)\n" + " 1000.0 LEVEL height of level (upper boundary)\n" + "=====================FORMER RECEPTOR FILE====================\n" + " 0 NUMRECEPTOR number of receptors\n" + "=====================FORMER SPECIES FILE=====================\n" + " 2 NUMTABLE number of variable properties. The following lines are fixed format\n" + "XXXX|NAME |decaytime |wetscava |wetsb|drydif|dryhenry|drya|partrho |parmean|partsig|dryvelo|weight |\n" + " AIRTRACER -999.9 -9.9E-09 -9.9 -9.9E09 -9.99 29.00\n" + " Cs-137 -999.9 1.0E-04 0.80 -9.9 2.5E03 6.0E-7 3.0E-1 -9.99 -9.99\n" + "=====================FORMER RELEEASES FILE===================\n" + " 1 NSPEC total number of species emitted\n" + " 0 EMITVAR 1 for emission variation \n" + " 1 LINK index of species in file SPECIES\n" + "1 NUMPOINT number of releases\n" + "20221030 160000 ID1, IT1 beginning date and time of release\n" + "20221031 160000 ID2, IT2 ending date and time of release\n" + " 113.144 XPOINT1 (real) longitude [deg] of lower left corner\n" + " 23.0467 YPOINT1 (real) latitude [deg] of lower left corner\n" + " 113.144 XPOINT2 (real) longitude [deg] of upper right corner\n" + " 23.0467 YPOINT2 (real) latitude [DEG] of upper right corner\n" + " 1 KINDZ (int) 1 for m above ground, 2 for m above sea level, 3 pressure\n" + " 0.0 ZPOINT1 (real) lower z-level\n" + " 10.000 ZPOINT2 (real) upper z-level\n" + " 10000 NPART (int) total number of particles to be released\n" + ".1000E+04 XMASS (real) total mass emitted, species 1\n" + "realease1 NAME OF RELEASE LOCATION\n"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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