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

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

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 C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx