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
No Match

`
`
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 = "^((?P<mode>[^:]+):\\s)?in:(?P<InputInterface>[^,]+)\\s+out:(?P<OutputInterface>[^,]+),\\sconnection-state:(?P<ConnectionState>[^\\s]+)\\s+(?:src-mac\\s+(?P<SourceMacAddress>[^,]+),\\s+)?proto\\s+(?P<Protocol>\\w+)(?:\\s+\\((?P<Flags>[^)]+)\\))?,\\s+\\[?(?P<SourceAddress>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|[a-f\\d:]+)\\]?(?::(?P<SourcePort>\\d+))?->\\[?(?P<DestinationAddress>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|[a-f\\d:]+)\\]?(?::(?P<DestinationPort>\\d+))?,\\s(NAT\\s?\\[?(?P<NatSourceAddress>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|[a-f\\d:]+)\\]?(?::(?P<NatSourcePort>\\d+))?->\\(\\[?(?P<NatExternalAddress>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|[a-f\\d:]+)\\]?(?::(?P<NatExternalPort>\\d+))?->\\[?(?P<NatInternalAddress>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|[a-f\\d:]+)\\]?(?::(?P<NatInternalPort>\\d+))?\\),\\s)?len\\s+(?P<Length>\\d+)"; final String string = "srcnat: in:(unknown 0) out:fbox, connection-state:new proto UDP, 82.62.133.323:123->224.0.1.1:123, len 96\n" + "srcnat: in:Interco out:WAN, connection-state:new src-mac 00:0c:49:16:06:aa, proto UDP, 10.1.40.1:59306->8.8.4.4:53, len 82\n" + "srcnat: in:Interco out:fbox, connection-state:new src-mac 00:0c:49:16:06:aa, proto ICMP (type 128, code 0), 2a0c:c621:151::2->2a0c:c621:151::1, len 192\n" + "output: in:(unknown 0) out:Interco, connection-state:established proto ICMP (type 128, code 0), 2a0c:b641:111::1->2a0c:b641:111::2, len 16\n" + "in:freebox out:Interco, connection-state:new,dnat src-mac 20:66:cf:18:cf:15, proto TCP (SYN), 18.130.12.138:51566->10.2.70.1:443, NAT 18.130.12.138:51566->(82.66.103.223:443->10.2.70.1:443), len 60\n" + "in:Wireguard-R0 out:(unknown 0), connection-state:established proto TCP (ACK,PSH), 10.255.1.1:179->10.255.1.2:34601, len 196\n" + "in:Wireguard-R0 out:(unknown 0), connection-state:established proto TCP (ACK), 10.255.1.1:179->10.255.1.2:34601, len 52\n" + "in:Wireguard-R0 out:(unknown 0), connection-state:established proto TCP (ACK,PSH), 10.255.1.1:179->10.255.1.2:34601, len 205\n" + "in:freebox out:Interco, connection-state:new,dnat src-mac 20:66:cf:18:cf:15, proto TCP (SYN), 18.130.12.138:39888->10.2.70.1:443, NAT 18.130.12.138:39888->(82.66.103.223:443->10.2.70.1:443), len 60\n" + "in:WAN out:Interco, connection-state:established,snat src-mac 80:2d:bf:8e:31:f7, proto UDP, [2620:2d:4000:1::41]:123->[2a0c:b641:112:20:250:56ff:fea9:578d]:41270, len 56\n" + "in:WAN out:Interco, connection-state:established,snat src-mac 80:2d:bf:39:f5:47, proto TCP (SYN,ACK), [2606:4700:4700::1111]:53->[2a0c:b641:112:40:20c:29ff:fe69:e5e7]:45257, len 40\n" + "srcnat: in:Interco out:WAN, connection-state:new src-mac 00:0c:29:16:06:aa, proto TCP (SYN), 10.1.40.1:34543->8.8.8.8:53, len 60\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