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

r"
"
mg

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"^((?:\[.+?:.*?\])+)(.*)$" test_str = ("[ar:胡彦斌]\n" "[ti:月光]\n" "[00:00.86]月光(秦时明月主题曲)\n" "[00:06.31]歌手 胡彦斌\n" "[00:08.68]作词 林文炫\n" "[00:10.49]作曲 胡彦斌\n" "[00:20.11]月光色\n" "[00:22.30]女子香\n" "[00:24.51]泪断剑\n" "[00:26.70]情多长\n" "[00:28.95]有多痛\n" "[00:30.97]无字想\n" "[00:33.35][00:33.35]忘了你\n" "[00:39.43]孤单魂\n" "[00:41.51]随风荡\n" "[00:43.57]谁去笑\n" "[00:45.87]痴情郎\n" "[00:48.03]这红尘的战场\n" "[00:52.95]千军万马有谁能称王\n" "[01:01.69]过情关\n" "[01:06.00]谁敢闯\n" "[01:10.42]望明月\n" "[01:15.62]心悲凉\n" "[01:19.13]千古恨\n" "[01:23.45]轮回尝\n" "[01:27.76]眼一闭\n" "[01:32.71]谁最狂\n" "[01:38.90]这世道的无常\n" "[01:43.23]注定敢爱的人一生伤\n" "[02:07.33]月光色\n" "[02:09.20]女子香\n" "[02:11.49]泪断剑\n" "[02:13.58]情多长\n" "[02:15.80]有多痛\n" "[02:17.87]无字想\n" "[02:20.23]忘了你\n" "[02:26.21]孤单魂\n" "[02:28.20]随风荡\n" "[02:30.56]谁去笑\n" "[02:32.64]痴情郎\n" "[02:34.94]这红尘的战场\n" "[02:39.70]千军万马有谁能称王\n" "[02:48.65]过情关\n" "[02:52.66]谁敢闯\n" "[02:57.34]望明月\n" "[03:02.44]心悲凉\n" "[03:05.97]千古恨\n" "[03:09.99]轮回尝\n" "[03:14.67]眼一闭\n" "[03:19.95]谁最狂\n" "[03:30.06]过情关\n" "[03:34.34]谁敢闯\n" "[03:38.78]望明月\n" "[03:43.92]心悲凉\n" "[03:47.58]千古恨\n" "[03:51.50]轮回尝\n" "[03:55.96]眼一闭\n" "[04:01.51]谁最狂\n" "[04:07.21]这世道的无常\n" "[04:21.34]注定敢爱的人一生伤") 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