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 = "\\!\\ninterface\\W[A-Z].*Ethernet(\\d)\\/(\\d)([^\\!]*)"; final String string = "Building configuration...\n\n" + "Current configuration : 3058 bytes\n" + "!\n" + "version 12.2\n" + "no service pad\n" + "service timestamps debug datetime msec\n" + "service timestamps log datetime msec\n" + "no service password-encryption\n" + "!\n" + "hostname cisco.test\n" + "!\n" + "boot-start-marker\n" + "boot-end-marker\n" + "!\n" + "!\n" + "username cisco password 0 cisco\n" + "!\n" + "!\n" + "aaa new-model\n" + "!\n" + "!\n" + "aaa authentication login default local enable\n" + "!\n" + "!\n" + "!\n" + "aaa session-id common\n" + "system mtu routing 1500\n" + "ip routing\n" + "no ip domain-lookup\n" + "ip domain-name cisco.test\n" + "ip dhcp excluded-address 172.29.1.200\n" + "ip dhcp excluded-address 172.29.1.1\n" + "!\n" + "ip dhcp pool USERS\n" + " network 172.29.1.0 255.255.255.0\n" + " default-router 172.29.1.1 \n" + "!\n" + "!\n" + "!\n" + "!\n" + "crypto pki trustpoint TP-self-signed-508261888\n" + " enrollment selfsigned\n" + " subject-name cn=IOS-Self-Signed-Certificate-508261888\n" + " revocation-check none\n" + " rsakeypair TP-self-signed-508261888\n" + "!\n" + "!\n" + "crypto pki certificate chain TP-self-signed-508261888\n" + " certificate self-signed 01\n" + " 3082023D 308201A6 A0030201 02020101 300D0609 2A864886 F70D0101 04050030 \n" + " 30312E30 2C060355 04031325 494F532D 53656C66 2D536967 6E65642D 43657274 \n" + " 69666963 6174652D 35303832 36313838 38301E17 0D393330 33303130 30303130 \n" + " 335A170D 32303031 30313030 30303030 5A303031 2E302C06 03550403 1325494F \n" + " 532D5365 6C662D53 69676E65 642D4365 72746966 69636174 652D3530 38323631 \n" + " 38383830 819F300D 06092A86 4886F70D 01010105 0003818D 00308189 02818100 \n" + " DAB963DF 1F90F0D1 0C422912 0788DC49 A3908659 CE3E9610 433233A5 1B1E00F5 \n" + " 3941C9E6 B9FC37C4 F1D39723 282E38FC E03A0384 9C386E0A 380FCE52 B77A1A69 \n" + " F62F2E8B FB570F83 3F0CDD21 058BE6BD A1B8BE37 4AFD3B02 E339CBA9 EB073555 \n" + " 63358466 8C7CFF88 D0F562CC B6C6CC0D 981CAB30 CB7CA2E5 CDD17A21 5AEC6F09 \n" + " 02030100 01A36730 65300F06 03551D13 0101FF04 05300301 01FF3012 0603551D \n" + " 11040B30 09820753 77697463 682E301F 0603551D 23041830 16801460 F6485305 \n" + " D274086D F29F51DE A3659656 752BB730 1D060355 1D0E0416 041460F6 485305D2 \n" + " 74086DF2 9F51DEA3 65965675 2BB7300D 06092A86 4886F70D 01010405 00038181 \n" + " 00590208 E0E292D7 B18ABD77 3D781313 A70B7896 46EE9079 1C66D967 B01D078C \n" + " A420A1D5 FE4C7CA9 EA89BC95 D0F2B0A4 4298B12C 3AB2DDC3 7608BD34 3FB41615 \n" + " 6833E4C3 52BDFDA2 DF7C4C9A B94A4745 386E8DB0 C4EB298A D5B00560 490EA5AF \n" + " 5B357860 E01D60F2 D9DBD62F 0922DF2B 11039A71 B23064EC CB4C846E DCCCDC4B A4\n" + " quit\n" + "!\n" + "!\n" + "!\n" + "spanning-tree mode pvst\n" + "spanning-tree extend system-id\n" + "!\n" + "vlan internal allocation policy ascending\n" + "!\n" + "ip ssh time-out 60\n" + "ip ssh authentication-retries 2\n" + "ip ssh version 2\n" + "!\n" + "!\n" + "interface FastEthernet0/1\n" + " switchport mode access\n" + " switchport port-security\n" + " switchport port-security mac-address sticky\n" + " shutdown\n" + "!\n" + "interface FastEthernet0/2\n" + "!\n" + "interface FastEthernet0/3\n" + "!\n" + "interface FastEthernet0/4\n" + "!\n" + "interface FastEthernet0/5\n" + "!\n" + "interface FastEthernet0/6\n" + "!\n" + "interface FastEthernet0/7\n" + "!\n" + "interface FastEthernet0/8\n" + "!\n" + "interface GigabitEthernet0/1\n" + "!\n" + "interface Vlan1\n" + " ip address 172.29.1.200 255.255.255.0\n" + "!\n" + "ip classless\n" + "ip http server\n" + "ip http secure-server\n" + "!\n" + "!\n" + "!\n" + "line con 0\n" + " length 0\n" + "line vty 0 4\n" + " privilege level 15\n" + " logging synchronous\n" + " length 0\n" + " transport input ssh\n" + "line vty 5 15\n" + " privilege level 15\n" + " logging synchronous\n" + " length 0\n" + " transport input ssh\n" + "!\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