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
No Match

/
/
gm

Test String

Substitution

Processing...

Code Generator

Generated Code

re = /(?<name>.+) (?<date>.+) Lat=\s*(?<lat>-?\d+\.\d*)\s*Lon=\s*(?<lon>-?\d+\.\d*) (?<dep>.+) (?<dt>.+) Moment Tensor: Expo=\d+\s*(?<cmt>.+) .+ .+ (?<faultPlane>.+strike=(?<strike>-?\d+\.?\d*)\s*dip=(?<dip>-?\d+\.?\d*)\s*slip=(?<slip>-?\d+\.?\d*))/m str = '010176A KERMADEC ISLANDS REGION Date: 1976/ 1/ 1 Centroid Time: 1:29:53.4 GMT Lat= -29.25 Lon=-176.96 Depth= 47.8 Half duration= 9.4 Centroid time minus hypocenter time: 13.8 Moment Tensor: Expo=26 7.680 0.090 -7.770 1.390 4.520 -3.260 Mw = 7.3 mb = 6.2 Ms = 0.0 Scalar Moment = 9.56e+26 Fault plane: strike=202 dip=30 slip=93 Fault plane: strike=18 dip=60 slip=88 010576A PERU Date: 1976/ 1/ 5 Centroid Time: 2:31:44.7 GMT Lat= -13.42 Lon= -75.14 Depth= 85.4 Half duration= 1.6 Centroid time minus hypocenter time: 8.4 Moment Tensor: Expo=24 -1.780 -0.590 2.370 -1.280 1.970 -2.900 Mw = 5.7 mb = 6.0 Ms = 0.0 Scalar Moment = 3.79e+24 Fault plane: strike=350 dip=28 slip=-60 Fault plane: strike=137 dip=66 slip=-105 010676A OFF EAST COAST OF KAMCHA Date: 1976/ 1/ 6 Centroid Time: 21: 8:25.1 GMT Lat= 51.45 Lon= 159.50 Depth= 15.0 Half duration= 2.8 Centroid time minus hypocenter time: 5.8 Moment Tensor: Expo=25 1.100 -0.300 -0.800 1.050 1.240 -0.560 Mw = 6.1 mb = 5.7 Ms = 6.0 Scalar Moment = 1.98e+25 Fault plane: strike=206 dip=18 slip=78 Fault plane: strike=39 dip=73 slip=94 010976A VANUATU ISLANDS Date: 1976/ 1/ 9 Centroid Time: 23:54:40.1 GMT Lat= -15.97 Lon= 167.81 Depth=173.7 Half duration= 3.5 Centroid time minus hypocenter time: 4.5 Moment Tensor: Expo=25 -1.700 2.290 -0.590 -2.330 -1.230 2.010 Mw = 6.3 mb = 6.1 Ms = 0.0 Scalar Moment = 3.64e+25 Fault plane: strike=246 dip=22 slip=-86 Fault plane: strike=62 dip=68 slip=-91 011376A ICELAND REGION Date: 1976/ 1/13 Centroid Time: 13:29:24.9 GMT Lat= 66.33 Lon= -16.29 Depth= 15.0 Half duration= 3.4 Centroid time minus hypocenter time: 5.4 Moment Tensor: Expo=25 -0.510 -2.860 3.370 0.050 -0.780 -0.860 Mw = 6.3 mb = 6.0 Ms = 6.4 Scalar Moment = 3.3e+25 Fault plane: strike=127 dip=82 slip=173 Fault plane: strike=218 dip=83 slip=9 011476A KERMADEC ISLANDS Date: 1976/ 1/14 Centroid Time: 15:56: 7.5 GMT Lat= -29.69 Lon=-177.04 Depth= 46.7 Half duration=20.0 Centroid time minus hypocenter time: 32.6 Moment Tensor: Expo=27 4.780 -0.490 -4.300 0.830 3.620 -1.320 Mw = 7.8 mb = 6.3 Ms = 0.0 Scalar Moment = 6.02e+27 Fault plane: strike=200 dip=26 slip=95 Fault plane: strike=15 dip=64 slip=88 011476B KERMADEC ISLANDS REGION Date: 1976/ 1/14 Centroid Time: 16:47:44.8 GMT Lat= -28.72 Lon=-176.75 Depth= 17.7 Half duration=20.5 Centroid time minus hypocenter time: 11.3 Moment Tensor: Expo=27 2.560 0.180 -2.740 3.580 6.770 -1.230 Mw = 7.9 mb = 6.5 Ms = 8.0 Scalar Moment = 8.18e+27 Fault plane: strike=189 dip=11 slip=71 Fault plane: strike=28 dip=80 slip=93 011476C KERMADEC ISLANDS REGION Date: 1976/ 1/14 Centroid Time: 22:43:50.7 GMT Lat= -28.61 Lon=-176.62 Depth= 15.0 Half duration= 3.7 Centroid time minus hypocenter time: 7.6 Moment Tensor: Expo=25 2.340 0.240 -2.580 0.620 3.710 -0.680 Mw = 6.4 mb = 5.5 Ms = 6.3 Scalar Moment = 4.54e+25 Fault plane: strike=197 dip=17 slip=97 Fault plane: strike=10 dip=73 slip=88 011576A KERMADEC ISLANDS REGION Date: 1976/ 1/15 Centroid Time: 6: 6:51.9 GMT Lat= -30.25 Lon=-176.63 Depth= 15.0 Half duration= 2.8 Centroid time minus hypocenter time: 5.8 Moment Tensor: Expo=25 1.440 0.060 -1.500 0.300 1.180 -0.460 Mw = 6.1 mb = 5.3 Ms = 6.2 Scalar Moment = 1.96e+25 Fault plane: strike=196 dip=26 slip=91 Fault plane: strike=15 dip=64 slip=89' subst = '\\n%% ${name}\\n% ${date}\\n% ${dep}\\n% ${dt}\\n% ${faultPlane}\\nFP = [${strike}, ${dip}, ${slip}];\\nCMT = [${cmt}];\\nLat = ${lat}; Lon = ${lon};\\nbb(CMT, Lon, Lat, dia, 1, \'r\');\\n' result = str.gsub(re, subst) # Print the result of the substitution puts 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 Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html