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

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "^((?:\\[.+?:.*?\\])+)(.*)$"; final String string = "[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]注定敢爱的人一生伤"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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