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

/
/
s

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 = "(?:(?:ok|changed|fatal): \\[)([A-Za-z0-9-.]+)(?:\\]) => \\{\\s+\"msg\": \\[(.*?)\\]\\s+\\}"; final String string = "PLAY [Core Switch] *************************************************************\n\n" + "TASK [Mapping additional variables] ********************************************\n" + "ok: [acm-lab-core1.expedient.com]\n\n" + "TASK [Setting the core number] *************************************************\n" + "ok: [acm-lab-core1.expedient.com]\n\n" + "TASK [Create directory for configuration files] ********************************\n" + "ok: [acm-lab-core1.expedient.com]\n\n" + "TASK [Render the trunk configuration] ******************************************\n" + "ok: [acm-lab-core1.expedient.com]\n\n" + "TASK [Render the rollback configuration] ***************************************\n" + "ok: [acm-lab-core1.expedient.com]\n\n" + "TASK [register the configuration rendered to a variable3] **********************\n" + "changed: [acm-lab-core1.expedient.com]\n\n" + "TASK [debug] *******************************************************************\n" + "ok: [acm-lab-core1.expedient.com] => {\n" + " \"msg\": [\n" + " \"interfaces {\", \n" + " \" ae10 {\", \n" + " \" unit 1001 {\", \n" + " \" encapsulation vlan-bridge;\", \n" + " \" vlan-id 1001;\", \n" + " \" }\", \n" + " \" unit 1002 {\", \n" + " \" encapsulation vlan-vpls;\", \n" + " \" vlan-id 1002;\", \n" + " \" }\", \n" + " \" }\", \n" + " \" irb {\", \n" + " \" unit 1001 {\", \n" + " \" description \\\\\\\\\\\"PUBLIC: Dunder Mifflin\\\\\\\\(/)\\\";\", \n" + " \" family inet {\", \n" + " \" address 206.166.155.2/29 {\", \n" + " \" vrrp-group 1001 {\", \n" + " \" virtual-address 206.166.155.1;\", \n" + " \" priority 200;\", \n" + " \" fast-interval 500;\", \n" + " \" no-preempt;\", \n" + " \" accept-data;\", \n" + " \" }\", \n" + " \" } \", \n" + " \" }\", \n" + " \" }\", \n" + " \" } \", \n" + " \"}\", \n" + " \"bridge-domains {\", \n" + " \" VLAN1001 {\", \n" + " \" vlan-id 1001;\", \n" + " \" interface ae10.1001;\", \n" + " \" routing-interface irb.1001;\", \n" + " \" }\", \n" + " \"}\", \n" + " \"routing-instances {\", \n" + " \" VPLS1002 {\", \n" + " \" instance-type vpls;\", \n" + " \" vlan-id 1002;\", \n" + " \" interface ae10.1002;\", \n" + " \" protocols {\", \n" + " \" vpls {\", \n" + " \" encapsulation-type ethernet-vlan;\", \n" + " \" interface ae10.1002;\", \n" + " \" no-tunnel-services;vpls-id 1062310;mtu 9216;\", \n" + " \" neighbor 206.210.84.1;\", \n" + " \" }\", \n" + " \" }\", \n" + " \" }\", \n" + " \"}\", \n" + " \"protocols {\", \n" + " \" bgp {\", \n" + " \" group DunderMiff-vsrx {\", \n" + " \" type external;\", \n" + " \" import [ tag-vsrx DunderMiff-in ];\", \n" + " \" export default-only;\", \n" + " \" peer-as 64512;\", \n" + " \" neighbor 206.166.155.5;\", \n" + " \" }\", \n" + " \" }\", \n" + " \"}\", \n" + " \"policy-options {\", \n" + " \" policy-statement DunderMiff-in {\", \n" + " \" term customer-networks { \", \n" + " \" from {\", \n" + " \" route-filter 2.2.1.0/24 exact;\", \n" + " \" route-filter 2.2.1.0/24 exact;\", \n" + " \" } \", \n" + " \" then accept; \", \n" + " \" } \", \n" + " \" term deny { \", \n" + " \" then reject; \", \n" + " \" } \", \n" + " \" } \", \n" + " \"}\"\n" + " ]\n" + "}\n\n" + "PLAY [Core Switch] *************************************************************\n\n" + "TASK [Mapping additional variables] ********************************************\n" + "ok: [acm-lab-core2.expedient.com]\n\n" + "TASK [Setting the core number] *************************************************\n" + "ok: [acm-lab-core2.expedient.com]\n\n" + "TASK [Create directory for configuration files] ********************************\n" + "ok: [acm-lab-core2.expedient.com]\n\n" + "TASK [Render the trunk configuration] ******************************************\n" + "ok: [acm-lab-core2.expedient.com]\n\n" + "TASK [Render the rollback configuration] ***************************************\n" + "ok: [acm-lab-core2.expedient.com]\n\n" + "TASK [register the configuration rendered to a variable3] **********************\n" + "changed: [acm-lab-core2.expedient.com]\n\n" + "TASK [debug] *******************************************************************\n" + "ok: [acm-lab-core2.expedient.com] => {\n" + " \"msg\": [\n" + " \"interfaces {\", \n" + " \" ae10 {\", \n" + " \" unit 1001 {\", \n" + " \" encapsulation vlan-bridge;\", \n" + " \" vlan-id 1001;\", \n" + " \" }\", \n" + " \" unit 1002 {\", \n" + " \" encapsulation vlan-vpls;\", \n" + " \" vlan-id 1002;\", \n" + " \" }\", \n" + " \" }\", \n" + " \" irb {\", \n" + " \" unit 1001 {\", \n" + " \" description \\\\\\\\\\\"PUBLIC: Dunder Mifflin\\\\\\\\(/)\\\";\", \n" + " \" family inet {\", \n" + " \" address 206.166.155.3/29 {\", \n" + " \" vrrp-group 1001 {\", \n" + " \" virtual-address 206.166.155.1;\", \n" + " \" priority 100;\", \n" + " \" fast-interval 500;\", \n" + " \" no-preempt;\", \n" + " \" accept-data;\", \n" + " \" }\", \n" + " \" } \", \n" + " \" }\", \n" + " \" }\", \n" + " \" } \", \n" + " \"}\", \n" + " \"bridge-domains {\", \n" + " \" VLAN1001 {\", \n" + " \" vlan-id 1001;\", \n" + " \" interface ae10.1001;\", \n" + " \" routing-interface irb.1001;\", \n" + " \" }\", \n" + " \"}\", \n" + " \"routing-instances {\", \n" + " \" VPLS1002 {\", \n" + " \" instance-type vpls;\", \n" + " \" vlan-id 1002;\", \n" + " \" interface ae10.1002;\", \n" + " \" protocols {\", \n" + " \" vpls {\", \n" + " \" encapsulation-type ethernet-vlan;\", \n" + " \" interface ae10.1002;\", \n" + " \" no-tunnel-services;vpls-id 1062310;mtu 9216;\", \n" + " \" neighbor 206.210.84.1;\", \n" + " \" }\", \n" + " \" }\", \n" + " \" }\", \n" + " \"}\", \n" + " \"protocols {\", \n" + " \" bgp {\", \n" + " \" group DunderMiff-vsrx {\", \n" + " \" type external;\", \n" + " \" import [ tag-vsrx DunderMiff-in ];\", \n" + " \" export default-only;\", \n" + " \" peer-as 64512;\", \n" + " \" neighbor 206.166.155.5;\", \n" + " \" }\", \n" + " \" }\", \n" + " \"}\", \n" + " \"policy-options {\", \n" + " \" policy-statement DunderMiff-in {\", \n" + " \" term customer-networks { \", \n" + " \" from {\", \n" + " \" route-filter 2.2.1.0/24 exact;\", \n" + " \" route-filter 2.2.1.0/24 exact;\", \n" + " \" } \", \n" + " \" then accept; \", \n" + " \" } \", \n" + " \" term deny { \", \n" + " \" then reject; \", \n" + " \" } \", \n" + " \" } \", \n" + " \"}\"\n" + " ]\n" + "}\n\n" + "PLAY [Spine Switch] ************************************************************\n\n" + "TASK [Mapping additional variables] ********************************************\n" + "ok: [acm-lab-spine1.expedient.com]\n\n" + "TASK [Render the trunk configuration] ******************************************\n" + "ok: [acm-lab-spine1.expedient.com]\n\n" + "TASK [Render the rollback configuration] ***************************************\n" + "ok: [acm-lab-spine1.expedient.com]\n\n" + "TASK [Display the configuration rendered] **************************************\n" + "changed: [acm-lab-spine1.expedient.com]\n\n" + "TASK [debug] *******************************************************************\n" + "ok: [acm-lab-spine1.expedient.com] => {\n" + " \"msg\": [\n" + " \"interfaces {\", \n" + " \" ae0 {\", \n" + " \" unit 1001 {\", \n" + " \" vlan-id 1001;\", \n" + " \" }\", \n" + " \" unit 1002 {\", \n" + " \" vlan-id 1002;\", \n" + " \" } \", \n" + " \" }\", \n" + " \" ae1 {\", \n" + " \" unit 1001 {\", \n" + " \" vlan-id 1001;\", \n" + " \" }\", \n" + " \" unit 1002 {\", \n" + " \" vlan-id 1002;\", \n" + " \" }\", \n" + " \" }\", \n" + " \"}\", \n" + " \"vlans {\", \n" + " \" VLAN1001 {\", \n" + " \" vlan-id 1001;\", \n" + " \" interface ae0.1001;\", \n" + " \" interface ae1.1001;\", \n" + " \" }\", \n" + " \" VLAN1002 {\", \n" + " \" vlan-id 1002;\", \n" + " \" interface ae0.1002;\", \n" + " \" interface ae1.1002;\", \n" + " \" } \", \n" + " \"}\"\n" + " ]\n" + "}\n\n" + "TASK [No Deploy] ***************************************************************\n" + "skipping: [acm-lab-spine1.expedient.com]\n\n" + "TASK [debug] *******************************************************************\n" + "skipping: [acm-lab-spine1.expedient.com]\n\n" + "PLAY [POD Switch] **************************************************************\n\n" + "TASK [Setting the interface range] *********************************************\n" + "ok: [acm-lab-pod1-sw.expedient.com]\n\n" + "TASK [Mapping additional variables] ********************************************\n" + "ok: [acm-lab-pod1-sw.expedient.com]\n\n" + "TASK [Render the trunk configuration] ******************************************\n" + "ok: [acm-lab-pod1-sw.expedient.com]\n\n" + "TASK [Render the rollback configuration] ***************************************\n" + "ok: [acm-lab-pod1-sw.expedient.com]\n\n" + "TASK [Display the configuration rendered] **************************************\n" + "changed: [acm-lab-pod1-sw.expedient.com]\n\n" + "TASK [debug] *******************************************************************\n" + "ok: [acm-lab-pod1-sw.expedient.com] => {\n" + " \"msg\": [\n" + " \"interfaces {\", \n" + " \" interface-range DVS {\", \n" + " \" unit 0 {\", \n" + " \" family ethernet-switching {\", \n" + " \" vlan {\", \n" + " \" members 1001;\", \n" + " \" members 1002;\", \n" + " \" members 1111;\", \n" + " \" }\", \n" + " \" }\", \n" + " \" }\", \n" + " \" } \", \n" + " \" ae0 {\", \n" + " \" unit 1001 {\", \n" + " \" vlan-id 1001;\", \n" + " \" }\", \n" + " \" unit 1002 {\", \n" + " \" vlan-id 1002;\", \n" + " \" } \", \n" + " \" }\", \n" + " \"}\", \n" + " \"vlans {\", \n" + " \" VLAN1001 {\", \n" + " \" vlan-id 1001;\", \n" + " \" interface ae0.1001;\", \n" + " \" }\", \n" + " \" VLAN1002 {\", \n" + " \" vlan-id 1002;\", \n" + " \" interface ae0.1002;\", \n" + " \" }\", \n" + " \" VLAN1111 {\", \n" + " \" vlan-id 1111;\", \n" + " \" }\", \n" + " \"}\"\n" + " ]\n" + "}\n\n" + "PLAY [151-fw - Radius Authentication] ******************************************\n\n" + "TASK [Setting the interface range] *********************************************\n" + "ok: [151-fw-mgmt.expedient.com]\n\n" + "TASK [Mapping additional variables] ********************************************\n" + "ok: [151-fw-mgmt.expedient.com]\n\n" + "TASK [Render the trunk configuration] ******************************************\n" + "changed: [151-fw-mgmt.expedient.com]\n\n" + "TASK [Render the rollback configuration] ***************************************\n" + "changed: [151-fw-mgmt.expedient.com]\n\n" + "TASK [Display the configuration rendered] **************************************\n" + "changed: [151-fw-mgmt.expedient.com]\n\n" + "TASK [debug] *******************************************************************\n" + "ok: [151-fw-mgmt.expedient.com] => {\n" + " \"msg\": [\n" + " \"security {\", \n" + " \" zones {\", \n" + " \" security-zone untrust {\", \n" + " \" address-book {\", \n" + " \" address dunderhead-vsrx 1.1.1.1/32;\", \n" + " \" address-set CUSTCBB-Auth-Expedient-Managed-Customer-FW {\", \n" + " \" address dunderhead-vsrx;\", \n" + " \" }\", \n" + " \" }\", \n" + " \" }\", \n" + " \" }\", \n" + " \"}\"\n" + " ]\n" + "}\n\n" + "PLAY [tdp-fw - Radius Authentication] ******************************************\n\n" + "TASK [Setting the interface range] *********************************************\n" + "ok: [tdp-fw-mgmt.expedient.com]\n\n" + "TASK [Mapping additional variables] ********************************************\n" + "ok: [tdp-fw-mgmt.expedient.com]\n\n" + "TASK [Render the trunk configuration] ******************************************\n" + "changed: [tdp-fw-mgmt.expedient.com]\n\n" + "TASK [Render the rollback configuration] ***************************************\n" + "changed: [tdp-fw-mgmt.expedient.com]\n\n" + "TASK [Display the configuration rendered] **************************************\n" + "changed: [tdp-fw-mgmt.expedient.com]\n\n" + "TASK [debug] *******************************************************************\n" + "ok: [tdp-fw-mgmt.expedient.com] => {\n" + " \"msg\": [\n" + " \"security {\", \n" + " \" zones {\", \n" + " \" security-zone untrust {\", \n" + " \" address-book {\", \n" + " \" address dunderhead-vsrx 1.1.1.1/32;\", \n" + " \" address-set CUSTCBB-Auth-Expedient-Managed-Customer-FW {\", \n" + " \" address dunderhead-vsrx;\", \n" + " \" }\", \n" + " \" }\", \n" + " \" }\", \n" + " \" }\", \n" + " \"}\"\n" + " ]\n" + "}\n\n" + "PLAY RECAP *********************************************************************\n" + "151-fw-mgmt.expedient.com : ok=6 changed=3 unreachable=0 failed=0 \n" + "acm-lab-core1.expedient.com : ok=7 changed=1 unreachable=0 failed=0 \n" + "acm-lab-core2.expedient.com : ok=7 changed=1 unreachable=0 failed=0 \n" + "acm-lab-pod1-sw.expedient.com : ok=6 changed=1 unreachable=0 failed=0 \n" + "acm-lab-spine1.expedient.com : ok=5 changed=1 unreachable=0 failed=0 \n" + "tdp-fw-mgmt.expedient.com : ok=6 changed=3 unreachable=0 failed=0 "; final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL); 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