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

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"(?<phase>[0-9]+([,|.]?[0-9]+)?)[\t](?<voltage>[0-9]+([,|.]?[0-9]+)?)[\t](?<fault>[0-9]+([,|.]?[0-9]+)?)[\t](?<PD>[0-9]+([,|.]?[0-9]+)?)" test_str = ("Fault location:\n" "Cable-length (m): 266\n" "Phase Voltage(kV) Fault(m): PD (pC):\n" "1 11.0 141.452 142.857 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_11kV_2016-08-17-11'04'05.loc\n" "1 11.0 141.452 190.476 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_11kV_2016-08-17-11'04'19.loc\n" "1 11.0 141.452 161.905 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_11kV_2016-08-17-11'04'19.loc\n" "1 11.0 139.673 171.429 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_11kV_2016-08-17-11'04'33.loc\n" "1 11.0 141.452 152.381 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_11kV_2016-08-17-11'04'33.loc\n" "1 13.0 140.562 295.238 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_13kV_2016-08-17-11'05'34.loc\n" "1 13.0 137.004 285.714 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_13kV_2016-08-17-11'05'34.loc\n" "1 13.0 140.562 238.095 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_13kV_2016-08-17-11'05'34.loc\n" "1 13.0 142.341 200.000 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_13kV_2016-08-17-11'05'34.loc\n" "1 13.0 140.562 333.333 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_13kV_2016-08-17-11'05'50.loc\n" "1 13.0 139.673 314.286 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_13kV_2016-08-17-11'05'50.loc\n" "3 13.0 136.114 228.571 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'39.loc\n" "1 13.0 136.114 171.429 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_13kV_2016-08-17-11'05'50.loc\n" "1 13.0 141.452 276.190 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_13kV_2016-08-17-11'06'06.loc\n" "1 13.0 139.673 180.952 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_13kV_2016-08-17-11'06'06.loc\n" "2 9.0 141.452 142.857 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_9kV_2016-08-17-11'08'58.loc\n" "2 11.0 136.114 304.762 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_11kV_2016-08-17-11'09'13.loc\n" "2 11.0 140.562 209.524 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_11kV_2016-08-17-11'09'13.loc\n" "2 11.0 138.783 285.714 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_11kV_2016-08-17-11'09'30.loc\n" "2 11.0 140.562 219.048 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_11kV_2016-08-17-11'09'30.loc\n" "2 11.0 139.673 333.333 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_11kV_2016-08-17-11'09'45.loc\n" "2 11.0 137.004 238.095 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_11kV_2016-08-17-11'09'45.loc\n" "2 13.0 140.562 447.619 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'00.loc\n" "2 13.0 139.673 352.381 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'00.loc\n" "2 13.0 137.893 323.810 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'00.loc\n" "2 13.0 134.335 171.429 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'00.loc\n" "2 13.0 140.562 447.619 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'16.loc\n" "2 13.0 140.562 419.048 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'16.loc\n" "2 13.0 138.783 390.476 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'16.loc\n" "2 13.0 138.783 304.762 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'16.loc\n" "2 13.0 140.562 200.000 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'16.loc\n" "2 13.0 140.562 447.619 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'41.loc\n" "2 13.0 138.783 409.524 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'41.loc\n" "2 13.0 139.673 361.905 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'41.loc\n" "2 13.0 137.893 352.381 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L2_13kV_2016-08-17-11'10'41.loc\n" "3 9.0 139.673 142.857 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_9kV_2016-08-17-11'13'10.loc\n" "3 11.0 120.101 342.857 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_11kV_2016-08-17-11'13'27.loc\n" "3 11.0 137.893 171.429 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_11kV_2016-08-17-11'13'27.loc\n" "3 11.0 139.673 219.048 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_11kV_2016-08-17-11'13'43.loc\n" "3 11.0 139.673 190.476 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_11kV_2016-08-17-11'13'43.loc\n" "3 11.0 139.673 304.762 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_11kV_2016-08-17-11'13'57.loc\n" "3 11.0 139.673 161.905 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_11kV_2016-08-17-11'13'57.loc\n" "3 13.0 137.893 380.952 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'11.loc\n" "3 13.0 140.562 352.381 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'11.loc\n" "3 13.0 139.673 352.381 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'11.loc\n" "3 13.0 137.893 333.333 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'11.loc\n" "3 13.0 140.562 200.000 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'11.loc\n" "3 13.0 139.673 428.571 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'25.loc\n" "3 13.0 140.562 409.524 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'25.loc\n" "3 13.0 137.004 323.810 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'25.loc\n" "3 13.0 136.114 209.524 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'25.loc\n" "3 13.0 120.101 628.571 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'39.loc\n" "3 13.0 140.562 314.286 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'39.loc\n" "3 13.0 137.893 266.667 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L3_13kV_2016-08-17-11'14'39.loc\n" "1 13.0 139.673 238.095 C:\\Users\\z2066\\Documents\\Munka\\Diagnosztika\\KD 2016\\OWTS\\Gyor regio\\Sopron\\Frankenburg BHTR-Kossuth KTW\\L1_13kV_2016-08-17-11'05'50.loc\n" "Joints:\n" "0\n" "266\n") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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 Python, please visit: https://docs.python.org/3/library/re.html