Regular Expressions 101

Save & Share

  • Regex Version: ver. 24
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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

r"
"

Test String

Substitution

Processing...

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"! Global keywords\n\n(.*?)(?=\n\s*\n)" test_str = ("\n" "!**************************************************************************************\n" "! Generated with OLGA version 2017.2.0\n" "!**************************************************************************************\n\n" "!**************************************************************************************\n" "! Global keywords\n" "!**************************************************************************************\n" "OPTIONS FLOWMODEL=OLGAHD\n" "CASE AUTHOR=Schlumberger, PROJECT=OLGA, TITLE=\"Basic Case\"\n" "FILES PVTFILE=./3phase.tab\n" "INTEGRATION ENDTIME=60 M, MAXDT=5 s, MINDT=0.01 s, STARTTIME=0 s, DTSTART=0.01 s\n" "OUTPUT DTOUT=1 h\n" "PROFILE DTPLOT=5 M\n" "TREND DTPLOT=15 s\n" "TRENDDATA VARIABLE=(VOLGBL, HT, GTSOUR)\n\n" "!**************************************************************************************\n" "! Library keywords\n" "!**************************************************************************************\n" "WALL LABEL=\"WALL-1\", THICKNESS=(0.009, 0.02, 0.02) m, MATERIAL=(\"MATER-1\", \"MATER-2\", \\\n" " \"MATER-2\")\n" "WALL LABEL=\"WALL-2\", THICKNESS=(0.0075, 0.02, 0.02) m, MATERIAL=(\"MATER-1\", \"MATER-2\", \\\n" " \"MATER-2\")\n" "MATERIAL LABEL=\"MATER-1\", CAPACITY=500 J/kg-C, CONDUCTIVITY=50 W/m-C, DENSITY=7850 kg/m3\n" "MATERIAL LABEL=\"MATER-2\", CAPACITY=880 J/kg-C, CONDUCTIVITY=1 W/m-C, DENSITY=2500 kg/m3\n\n" "!**************************************************************************************\n" "! Network Component\n" "!**************************************************************************************\n" "NETWORKCOMPONENT TYPE=FLOWPATH, TAG=FP_PIPELINE\n" " PARAMETERS LABEL=PIPELINE\n" " GEOMETRY LABEL=\"GEOM-1\"\n" " PIPE ROUGHNESS=5E-05 m, LABEL=\"PIPE-1\", WALL=\"WALL-1\", NSEGMENT=10, LENGTH=400 m, \\\n" " ELEVATION=10 m, DIAMETER=0.12 m\n" " BRANCH FLUID=Fluid1\n" " HEATTRANSFER LABEL=\"HEATTRANS-1\", TAMBIENT=6 C, HAMBIENT=6.5 W/M2-C\n" " SOURCE LABEL=\"SOURCE-1\", TIME=(0, 10, 20, 30, 40) M, PIPE=\"PIPE-1\", SECTION=1, \\\n" " TEMPERATURE=(62, 62, 62, 62, 62) C, MASSFLOW=(18, 18, 9, 9, 18) KG/S\n" " TRENDDATA PIPE=\"PIPE-1\", SECTION=1, VARIABLE=(HOL, HOLWT, ID, PT, Q2, TM, TU)\n" " TRENDDATA VARIABLE=(OILC, WATC, GASC, OILCFR, WATCFR, GASCFR)\n" " PROFILEDATA VARIABLE=(HOL, PT, TM, QLT, QG, Q2, ID, QLTWT)\n" " SERVERDATA PIPE=\"PIPE-1\", SECTION=1, VARIABLE=(HOL, HOLWT, PT, TM)\n" " SERVERDATA VARIABLE=(HOL, HOLWT, PT, TM)\n" " SERVERDATA VARIABLE=(QLT, QG, QLTWT, ID)\n" " TRENDDATA PIPE=\"PIPE-1\", SECTION=11, VARIABLE=(QT, QG, QLT, QLTHL, QLTWT, QGST, \\\n" " QOST, QWTST, GT, GG, GL)\n" "ENDNETWORKCOMPONENT\n\n" "!**************************************************************************************\n" "! Network Component\n" "!**************************************************************************************\n" "NETWORKCOMPONENT TYPE=NODE, TAG=NODE_INLET\n" " PARAMETERS LABEL=INLET, TYPE=CLOSED\n" "ENDNETWORKCOMPONENT\n\n" "!**************************************************************************************\n" "! Network Component\n" "!**************************************************************************************\n" "NETWORKCOMPONENT TYPE=NODE, TAG=NODE_OUTLET\n" " PARAMETERS LABEL=OUTLET, TYPE=PRESSURE, TEMPERATURE=22 C, PRESSURE=5000000 Pa, \\\n" " FLUID=Fluid1\n" "ENDNETWORKCOMPONENT\n\n" "!**************************************************************************************\n" "! Connections\n" "!**************************************************************************************\n" "CONNECTION TERMINALS = (FP_PIPELINE INLET, NODE_INLET FLOWTERM_1)\n" "CONNECTION TERMINALS = (FP_PIPELINE OUTLET, NODE_OUTLET FLOWTERM_1)\n\n" "ENDCASE\n") subst = "" # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, subst, test_str, 1) if result: print (result) # 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