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

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