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