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

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 = "\\[[0-9]+[;]?[1H\\[K\\[7mlines [0-9-]+ \\[[0-9]+m\\[60;1H\\[60;1H"; final String string = "lines 17464-17522  140.90.101.62-NOAA {\n" + " ip-netmask 140.90.101.62;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA National Center for Environmental Prediction - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.172.138.17-NOAA {\n" + " ip-netmask 140.172.138.17;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA Operational Model Archive and Distro System (NOMADS-HTTPS) National Center for Environmental Prediction (NCEP-FTP) - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 128.132.41.26-AFRL {\n" + " ip-netmask 128.132.41.26;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - CAOC AF Research Lab - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.172.138.18-NOAA {\n" + " ip-netmask 140.172.138.18;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA Operational Model Archive and Distro System - HTTPS - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 204.0.3.146 {\n" + " ip-netmask 204.0.3.146;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - Intellicast - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 192.145.239.17 {\n" + " ip-netmask 192.145.239.17;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - Hurrtrack - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 70.39.145.13 {\n" + " ip-netmask 70.39.145.13;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - Hurrtrack - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 204.0.3.147 {\n" + " ip-netmask 204.0.3.147;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - Intellicast - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.172.17.191-NOAA {\n" + " ip-netmask 140.172.17.191;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA Aviation Weather Center - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.90.200.191-NOAA {\n" + " ip-netmask 140.90.200.191;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA Aviation Weather Center - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 129.15.96.191-NOAA {\n" + " ip-netmask 129.15.96.191;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA Aviation Weather Center - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 23.3.96.193-NOAA {\n" + " ip-netmask 23.3.96.193;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA - NWS Radar Sites - Contact WCOC DSN 271-2586\";\n" + "lines 17523-17581  }\n" + " 205.156.4.51-NOAA {\n" + " ip-netmask 205.156.4.51;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA - Web Map Service - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 23.3.96.242-NOAA {\n" + " ip-netmask 23.3.96.242;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA - NWS Radar Sites - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 199.212.17.28 {\n" + " ip-netmask 199.212.17.28;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - Canadian Weather - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 199.212.17.39 {\n" + " ip-netmask 199.212.17.39;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - Canadian Weather - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 192.139.6.68 {\n" + " ip-netmask 192.139.6.68;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - Canadian Northern Forestry Center - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.90.101.79-NOAA {\n" + " ip-netmask 140.90.101.79;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA NWS/NCEP TGFTP server - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.90.101.34-NOAA {\n" + " ip-netmask 140.90.101.34;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA NWS/NCEP FTP HPC Anony server - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.90.101.132-NOAA {\n" + " ip-netmask 140.90.101.132;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA NWS/NCEP Wind/Wave server - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.172.138.79-NOAA {\n" + " ip-netmask 140.172.138.79;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA NWS/NCEP TGFTP server - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.90.213.161-NOAA {\n" + " ip-netmask 140.90.213.161;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA NESDIS server - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.90.195.198-NOAA {\n" + " ip-netmask 140.90.195.198;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA National Center for Environmental Prediction - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.90.192.168-NOAA {\n" + " ip-netmask 140.90.192.168;\n" + " tag GLOBAL-WEATHER;\n" + "lines 17582-17640  description \"Weather - NOAA National Center for Environmental Prediction - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 128.118.28.249-PSU {\n" + " ip-netmask 128.118.28.249;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - Penn State Uni Bufkit - apps2.meteo.psu.edu - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.90.128.71-NOAA {\n" + " ip-netmask 140.90.128.71;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA National Center for Environmental Prediction - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.90.88.142-NOAA {\n" + " ip-netmask 140.90.88.142;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA National Center for Environmental Prediction - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.90.33.41-NOAA {\n" + " ip-netmask 140.90.33.41;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA National Center for Environmental Prediction - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 140.90.33.31-NOAA {\n" + " ip-netmask 140.90.33.31;\n" + " tag GLOBAL-WEATHER;\n" + " description \"Weather - NOAA National Center for Environmental Prediction - Contact WCOC DSN 271-2586\";\n" + " }\n" + " 144.183.222.21 {\n" + " ip-netmask 144.183.222.21;\n" + " }\n" + " 144.183.32.21 {\n" + " ip-netmask 144.183.32.21;\n" + " }\n" + " 199.124.100.0_22 {\n" + " ip-netmask 199.124.100.0/22;\n" + " }\n" + " 199.124.104.0_23 {\n" + " ip-netmask 199.124.104.0/23;\n" + " }\n" + " 199.124.96.15-199.124.96.18 {\n" + " ip-range 199.124.96.15-199.124.96.18;\n" + " }\n" + " 199.225.7.15 {\n" + " ip-netmask 199.225.7.15;\n" + " }\n" + " 214.2.155.12-214.2.155.231 {\n" + " ip-range 214.2.155.12-214.2.155.231;\n" + " }\n" + " 214.2.2.245 {\n" + " ip-netmask 214.2.2.245;\n" + " }\n" + " 214.2.2.246 {\n" + " ip-netmask 214.2.2.246;\n" + " }\n" + " 214.2.90.222 {\n" + " ip-netmask 214.2.90.222;\n" + " }\n" + " 134.11.168.17 {\n" + " ip-netmask 134.11.168.17;\n" + "lines 17641-17699  description \"Pentagon Army ROVR destination\";\n" + " }"; 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