Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
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
Processing...

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*)( lb \\(\\d*\\.*\\d* kg\\))"; final String string = "Age Weight Length\n" + "0 7.4 lb (3.3 kg) 19.6\" (49.8 cm)\n" + "1 21.3 lb (9.6 kg) 29.8\" (75.7 cm)\n" + "2 27.5 lb (12.5 kg) 34.2\" (86.8 cm)\n" + "3 31.0 lb (14.0 kg) 37.5\" (95.2 cm)\n" + "4 36.0 lb (16.3 kg) 40.3\" (102.3 cm)\n" + "5 40.5 lb (18.4 kg) 43.0\" (109.2 cm)\n" + "6 45.5 lb (20.6 kg) 45.5\" (115.5 cm)\n" + "7 50.5 lb (22.9 kg) 48.0\" (121.9 cm)\n" + "8 56.5 lb (25.6 kg) 50.4\" (128 cm)\n" + "9 63.0 lb (28.6 kg) 52.5\" (133.3 cm)\n" + "10 70.5 lb (32 kg) 54.5\" (138.4 cm)\n" + "11 78.5 lb (35.6 kg) 56.5\" (143.5 cm)\n" + "12 88.0 lb (39.9 kg) 58.7\" (149.1 cm)\n" + "13 100.0 lb (45.3 kg) 61.5\" (156.2 cm)\n" + "14 112.0 lb (50.8 kg) 64.5\" (163.8 cm)\n" + "15 123.5 lb (56.0 kg) 67.0\" (170.1 cm)\n" + "16 134.0 lb (60.8 kg) 68.3\" (173.4 cm)\n" + "17 142.0 lb (64.4 kg) 69.0\" (175.2 cm)\n" + "18 147.5 lb (66.9 kg) 69.2\" (175.7 cm)\n" + "19 152.0 lb (68.9 kg) 69.5\" (176.5 cm)\n" + "20 155.0 lb (70.3 kg) 69.7\" (177 cm)\n"; final String subst = "\\1"; 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