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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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"
"

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 = "(address=[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+) (mac-address=[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}) .*(host-name=.*)"; final String string = " 0 address=172.31.10.254 mac-address=00:02:03:07:91:26 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=11h38m39s last-seen=12h21m21s active-address=172.31.10.254 active-mac-address=00:02:03:07:91:26 active-server=DHCP_CENTRALPACK host-name=Desenvol-vimento\n" + " 1 address=172.31.10.253 mac-address=00:18:4D:02:81:35 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=14h17m50s last-seen=9h42m10s active-address=172.31.10.253 active-mac-address=00:18:4D:02:81:35 active-server=DHCP_CENTRALPACK host-name=Estoque-bicolor\n" + " 2 address=172.31.10.252 mac-address=74:86:7A:F6:2A:16 address-lists=\"\" server=DHCP_CENTRALPACK always-broadcast=yes dhcp-option=\"\" status=bound expires-after=22h41m28s last-seen=1h18m32s active-address=172.31.10.252 active-mac-address=74:86:7A:F6:2A:16 active-client-id=1:74:86:7a:f6:2a:16 active-server=DHCP_CENTRALPACK host-name=CTPMCFA23\n" + " 3 address=172.31.10.251 mac-address=D4:AE:52:FD:9C:B5 address-lists=\"\" server=DHCP_CENTRALPACK always-broadcast=yes dhcp-option=\"\" status=bound expires-after=20h19m36s last-seen=30m14s active-address=172.31.10.251 active-mac-address=D4:AE:52:FD:9C:B5 active-client-id=1:d4:ae:52:fd:9c:b5 active-server=DHCP_CENTRALPACK host-name=PRTMC1344\n" + " 4 address=172.31.10.250 mac-address=9C:B6:54:3A:41:C3 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" last-seen=6w2d3h57m24s host-name=ctpimfa30\n" + " 5 address=172.31.10.248 mac-address=10:1F:74:3C:07:2C address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound last-seen=3h26m6s active-address=172.31.10.248 active-mac-address=10:1F:74:3C:07:2C active-client-id=1:10:1f:74:3c:7:2c active-server=DHCP_CENTRALPACK host-name=NPI3C072C\n" + " 6 address=172.31.10.247 mac-address=00:18:4D:02:81:34 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=16h12m2s last-seen=7h47m58s active-address=172.31.10.247 active-mac-address=00:18:4D:02:81:34 active-server=DHCP_CENTRALPACK host-name=estoque-flexo\n" + " 7 address=172.31.10.246 mac-address=00:1E:C9:30:00:74 address-lists=\"\" server=DHCP_CENTRALPACK always-broadcast=yes dhcp-option=\"\" status=bound expires-after=20h20m50s last-seen=1h32m36s active-address=172.31.10.246 active-mac-address=00:1E:C9:30:00:74 active-client-id=1:0:1e:c9:30:0:74 active-server=DHCP_CENTRALPACK host-name=PRTMC131D\n" + " 8 address=172.31.10.245 mac-address=90:B1:1C:F7:0B:9D address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=20h50m6s last-seen=1h46m37s active-address=172.31.10.245 active-mac-address=90:B1:1C:F7:0B:9D active-client-id=1:90:b1:1c:f7:b:9d active-server=DHCP_CENTRALPACK host-name=CTPMCFA1C\n" + " 9 address=172.31.10.244 mac-address=00:19:BB:61:81:06 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=20h40m15s last-seen=1h18m41s active-address=172.31.10.244 active-mac-address=00:19:BB:61:81:06 active-client-id=1:0:19:bb:61:81:6 active-server=DHCP_CENTRALPACK host-name=prtmc1317\n" + "10 address=172.31.10.243 mac-address=74:86:7A:FC:E2:37 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" last-seen=9w3d1h11m31s host-name=PRTNB02FB\n" + "11 address=172.31.10.242 mac-address=A0:2B:B8:00:A0:7A address-lists=\"\" server=DHCP_CENTRALPACK always-broadcast=yes dhcp-option=\"\" status=bound expires-after=20h31m40s last-seen=13s active-address=172.31.10.242 active-mac-address=A0:2B:B8:00:A0:7A active-client-id=1:a0:2b:b8:0:a0:7a active-server=DHCP_CENTRALPACK host-name=CTPMCFA2B\n" + "12 address=172.31.10.241 mac-address=00:18:8B:E0:FD:54 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=20h50m25s last-seen=50m9s active-address=172.31.10.241 active-mac-address=00:18:8B:E0:FD:54 active-client-id=1:0:18:8b:e0:fd:54 active-server=DHCP_CENTRALPACK host-name=CTPMC1312\n" + "13 address=172.31.10.240 mac-address=D4:AE:52:FD:9C:B2 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=21h52m41s last-seen=43m10s active-address=172.31.10.240 active-mac-address=D4:AE:52:FD:9C:B2 active-client-id=1:d4:ae:52:fd:9c:b2 active-server=DHCP_CENTRALPACK host-name=PRTMC1346\n" + "14 address=172.31.10.239 mac-address=00:C0:EE:94:1E:0B address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=20h32m1s last-seen=3h27m59s active-address=172.31.10.239 active-mac-address=00:C0:EE:94:1E:0B active-client-id=1:0:c0:ee:94:1e:b active-server=DHCP_CENTRALPACK\n" + "15 address=172.31.10.238 mac-address=00:19:BB:61:71:A4 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=17h57m28s last-seen=55m1s active-address=172.31.10.238 active-mac-address=00:19:BB:61:71:A4 active-client-id=1:0:19:bb:61:71:a4 active-server=DHCP_CENTRALPACK host-name=CTPMC0F09\n" + "16 address=172.31.10.237 mac-address=90:B1:1C:F7:0A:39 address-lists=\"\" server=DHCP_CENTRALPACK always-broadcast=yes dhcp-option=\"\" status=bound expires-after=20h16m33s last-seen=22m40s active-address=172.31.10.237 active-mac-address=90:B1:1C:F7:0A:39 active-client-id=1:90:b1:1c:f7:a:39 active-server=DHCP_CENTRALPACK host-name=PRTMC0139\n"; final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(string); if (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