Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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

/
/
igm

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 = "\\b(^(http|https|wss|ws|ftp|ftps):\\/\\/127[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|^(http|https|wss|ws|ftp|ftps):\\/\\/0.0.0.0|^(http|https|wss|ws|ftp|ftps):\\/\\/(10)([.](25[0-5]|2[0-4][0-9]|1[0-9]{1,2}|[0-9]{1,2})){3}|^(http|https|wss|ws|ftp|ftps):\\/\\/localhost|^(http|https|wss|ws|ftp|ftps):\\/\\/172[.](0?16|0?17|0?18|0?19|0?20|0?21|0?22|0?23|0?24|0?25|0?26|0?27|0?28|0?29|0?30|0?31)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|^(http|https|wss|ws|ftp|ftps):\\/\\/192[.]168[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|^(http|https|wss|ws|ftp|ftps):\\/\\/169[.]254[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:\\/([789]|1?[0-9]{2}))?\\b"; final String string = "# loopback\n" + "http://127.0.0.0:80\n" + "https://127.0.0.0000 # only three digits supported here\n\n" + "# 0.0.0.0 Day\n" + "http://0.0.0.0\n" + "ws://0.0.0.0\n\n" + "# Localhost\n" + "http://localhost:25\n" + "https://localhost:8080\n" + "HTTP://LoCALhosT:99 # Case insensitive\n" + "ws://localhost:7777\n" + "wss://LOCALHOST:9200\n" + "https://google.com/blah/localhost/ # This should not alert\n\n" + "# Web Sockets\n" + "ws://localhost:2121/sockjs-node\n" + "wss://localhost:5882/\n\n" + "# FTP\n" + "ftp://192.168.0.1\n" + "ftps://127.0.0.1\n" + "ftps://localhost\n\n" + "# A (/8 CIDR)\n" + "wss://10.0.0.1/10\n" + "ws://10.255.22.33\n\n" + "# B (/12 CIDR)\n" + "http://172.17.100.155/32 # 172.16.0.0 to 172.31.255.255\n" + "http://172.15.1.1 # This should not fire\n" + "http://172.32.1.1 # This should not fire\n\n" + "# C (/16 CIDR)\n" + "https://192.168.1.1/random/path\n" + "wss://192.168.1.255\n" + "http://192.168.9.9 \n\n" + "# IPv6\n" + "http://fc00:833e:d648:f196:5c6c:1d9a:a14b:0d59 # Private IPv6\n" + "wss://fc01:67e5::6a66:34a7\n" + "ftp://FC02:5fcf:e093:bb10:ce77:b5c9::/112\n" + "http://fc03:49bd:b7c1:5685:fa0a:87e9::2/128\n" + "ws://fc04:::2 hmpf\n" + "http://0de9:b022:883f:2c3c:7dba:a184:7576:4531 # public IP addresses\n" + "https://04e4:e53b:0c2f:2990:27ad:0464:6dd3:b6b3\n" + "wss://0319:3b4a:546e:d480:4814:f885:3396:bba2\n" + "ws://5394:03f5:606f:273e:e343:d94a:610a:3f2e\n" + "ftp://63cd:ad1f:7ffb:62c7:cc17:6e20:9b59:2f7d\n\n" + "# IPV6 with brackets\n" + "http://[::1] # Private IPv6\n" + "http://[fc00:833e:d648:f196:5c6c:1d9a:a14b:0d59]\n" + "wss://[fc01:67e5::6a66:34a7]\n" + "ftp://[FC02:5fcf:e093:bb10:ce77:b5c9::]/112\n" + "http://[fc03:49bd:b7c1:5685:fa0a:87e9::2]/128\n" + "ws://[fc04:::2] hmpf\n" + "http://[0de9:b022:883f:2c3c:7dba:a184:7576:4531] # public IP addresses None of these should fire\n" + "https://[04e4:e53b:0c2f:2990:27ad:0464:6dd3:b6b3]\n" + "wss://[0319:3b4a:546e:d480:4814:f885:3396:bba2]\n" + "ws://[5394:03f5:606f:273e:e343:d94a:610a:3f2e]\n" + "ftp://[63cd:ad1f:7ffb:62c7:cc17:6e20:9b59:2f7d]\n\n" + "# More \"crap\" should not fire\n" + "ws://30.168.1.255.1\n" + "wss://127.1\n" + "https://192.168.1.256\n" + "http://-1.2.3.4\n" + "wss://1.1.1.1.\n" + "ftp://3...3\n" + "wss://255.255.255.255\n" + "ftps://0.0.0.0\n" + "wss://1.1.1.01\n" + "https://10207287.fls.doubleclick.net/\n\n" + "# decimal vs. octal mix\n" + "http://172.031.33.33\n" + "http://172.021.133.01\n\n" + "# octal - not supported here\n" + "http://0254.0021.0062.0041 # Should not fire\n\n" + "# link-local addresses IPv4\n" + "http://169.254.1.0\n" + "http://169.254.254.255\n\n" + "# link-local addresses IPv6\n" + "http://fe90:1234::/64\n" + "http://feaf:ffff::/128\n" + "http://febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n"; final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | 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