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

/
/
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 = "^.*_(.*)\\/.*?(\\d\\d?)-(\\d\\d?)-(\\d\\d?)-(\\d).*total images\\/sec: (\\d*).(\\d*)$"; final String string = "tf_colocation_sync_alexnet/1-1-1-1//logs_han0001i_worker:total images/sec: 126.29\n" + "tf_colocation_sync_alexnet/2-2-2-1//logs_han0031i_worker:total images/sec: 284.09\n" + "tf_colocation_sync_alexnet/3-3-3-1//logs_han0028i_worker:total images/sec: 380.20\n" + "tf_colocation_sync_alexnet/4-4-4-1//logs_han0028i_worker:total images/sec: 433.71\n" + "tf_colocation_sync_alexnet/5-5-5-1//logs_han0031i_worker:total images/sec: 563.62\n" + "tf_colocation_sync_alexnet/6-6-6-1//logs_han0028i_worker:total images/sec: 650.25\n" + "tf_colocation_sync_alexnet/7-7-7-1//logs_han0028i_worker:total images/sec: 714.17\n" + "tf_colocation_sync_alexnet/8-8-8-1//logs_han0021i_worker:total images/sec: 771.35\n" + "tf_colocation_sync_googlenet/1-1-1-1//logs_han0001i_worker:total images/sec: 103.20\n" + "tf_colocation_sync_googlenet/2-2-2-1//logs_han0036i_worker:total images/sec: 195.63\n" + "tf_colocation_sync_googlenet/3-3-3-1//logs_han0031i_worker:total images/sec: 296.70\n" + "tf_colocation_sync_googlenet/4-4-4-1//logs_han0040i_worker:total images/sec: 397.95\n" + "tf_colocation_sync_googlenet/5-5-5-1//logs_han0006i_worker:total images/sec: 501.80\n" + "tf_colocation_sync_googlenet/6-6-6-1//logs_han0033i_worker:total images/sec: 601.88\n" + "tf_colocation_sync_googlenet/7-7-7-1//logs_han0031i_worker:total images/sec: 694.32\n" + "tf_colocation_sync_googlenet/8-8-8-1//logs_han0013i_worker:total images/sec: 793.94\n" + "tf_colocation_sync_inception3/1-1-1-1//logs_han0001i_worker:total images/sec: 27.95\n" + "tf_colocation_sync_inception3/2-2-2-1//logs_han0032i_worker:total images/sec: 57.89\n" + "tf_colocation_sync_inception3/3-3-3-1//logs_han0035i_worker:total images/sec: 86.88\n" + "tf_colocation_sync_inception3/4-4-4-1//logs_han0036i_worker:total images/sec: 115.63\n" + "tf_colocation_sync_inception3/5-5-5-1//logs_han0040i_worker:total images/sec: 145.01\n" + "tf_colocation_sync_inception3/6-6-6-1//logs_han0008i_worker:total images/sec: 173.67\n" + "tf_colocation_sync_inception3/7-7-7-1//logs_han0031i_worker:total images/sec: 203.46\n" + "tf_colocation_sync_inception3/8-8-8-1//logs_han0035i_worker:total images/sec: 230.64\n" + "tf_colocation_sync_resnet152/1-1-1-1//logs_han0001i_worker:total images/sec: 14.61\n" + "tf_colocation_sync_resnet152/2-2-2-1//logs_han0032i_worker:total images/sec: 28.89\n" + "tf_colocation_sync_resnet152/3-3-3-1//logs_han0023i_worker:total images/sec: 44.70\n" + "tf_colocation_sync_resnet152/4-4-4-1//logs_han0031i_worker:total images/sec: 58.99\n" + "tf_colocation_sync_resnet152/5-5-5-1//logs_han0007i_worker:total images/sec: 73.51\n" + "tf_colocation_sync_resnet152/6-6-6-1//logs_han0026i_worker:total images/sec: 85.85\n" + "tf_colocation_sync_resnet152/7-7-7-1//logs_han0031i_worker:total images/sec: 102.79\n" + "tf_colocation_sync_resnet152/8-8-8-1//logs_han0035i_worker:total images/sec: 117.12\n" + "tf_colocation_sync_resnet50/1-1-1-1//logs_han0001i_worker:total images/sec: 44.83\n" + "tf_colocation_sync_resnet50/2-2-2-1//logs_han0040i_worker:total images/sec: 90.92\n" + "tf_colocation_sync_resnet50/3-3-3-1//logs_han0004i_worker:total images/sec: 138.23\n" + "tf_colocation_sync_resnet50/4-4-4-1//logs_han0008i_worker:total images/sec: 185.06\n" + "tf_colocation_sync_resnet50/5-5-5-1//logs_han0032i_worker:total images/sec: 233.60\n" + "tf_colocation_sync_resnet50/6-6-6-1//logs_han0031i_worker:total images/sec: 276.48\n" + "tf_colocation_sync_resnet50/7-7-7-1//logs_han0008i_worker:total images/sec: 323.27\n" + "tf_colocation_sync_resnet50/8-8-8-1//logs_han0035i_worker:total images/sec: 367.67\n" + "tf_colocation_sync_vgg16/1-1-1-1//logs_han0001i_worker:total images/sec: 7.85\n" + "tf_colocation_sync_vgg16/2-2-2-1//logs_han0035i_worker:total images/sec: 16.75\n" + "tf_colocation_sync_vgg16/3-3-3-1//logs_han0040i_worker:total images/sec: 23.14\n" + "tf_colocation_sync_vgg16/4-4-4-1//logs_han0037i_worker:total images/sec: 27.65\n" + "tf_colocation_sync_vgg16/5-5-5-1//logs_han0031i_worker:total images/sec: 32.54\n" + "tf_colocation_sync_vgg16/6-6-6-1//logs_han0008i_worker:total images/sec: 37.42\n" + "tf_colocation_sync_vgg16/7-7-7-1//logs_han0034i_worker:total images/sec: 39.26\n" + "tf_colocation_sync_vgg16/8-8-8-1//logs_han0040i_worker:total images/sec: 40.95"; final String subst = "\\1\\t\\2\\t\\3\\t\\4\\t\\5\\t\\6,\\7"; 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