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

/
/
gm

Test String

Substitution

Processing...

Code Generator

Generated Code

// include the latest version of the regex crate in your Cargo.toml extern crate regex; use regex::Regex; fn main() { let regex = Regex::new(r#"(?m)("log":".*?(?P<message>Company\s*[^\.]+.)\s*Company:\s*\[(?P<company>[0-9]+)\]\.\s*Rate:\s*\[(?P<rate>[0-9\.B\\\/s]+)\])"#).unwrap(); let string = "{\"kubernetes\":{\"pod_name\":\"throttler-0\",\"namespace_name\":\"default\",\"pod_id\":\"4f91ad3a-dd6f-4d56-b08a-2225edd67f9b\",\"labels\":{\"CX_AZ\":\"us-east-2a\",\"CX_CLOUDFLARE_SITE_ID\":\"ddb3ffb7b6beb645b97e4bab3fbcfc44\",\"CX_CLUSTER_DNS\":\"usprod1.us-east-2.k8s-prod.coralogix.us\",\"CX_DEPLOYABLE\":\"default\",\"CX_DOMAIN\":\"coralogix.us\",\"CX_ENV_CLASS\":\"usprod1\",\"CX_ENV_ID\":\"usprod1\",\"CX_ENV_TYPE\":\"prod\",\"CX_ORG\":\"coralogix\",\"CX_PRODUCT\":\"logs-data\",\"CX_REGION\":\"us-east-2\",\"CX_REPOSITORY\":\"throttler\",\"CX_SERVICE_NAME\":\"throttler\",\"CX_SERVICE_TYPE\":\"application\",\"app_kubernetes_io_instance\":\"throttler\",\"app_kubernetes_io_managed-by\":\"Helm\",\"app_kubernetes_io_name\":\"throttler\",\"controller-revision-hash\":\"throttler-8b99fb774\",\"pipelines.coralogix.net\\/managed-by\":\"eng-pipelines\",\"pipelines.coralogix.net\\/pipelines-version\":\"0.3.42\",\"statefulset.kubernetes.io\\/pod-name\":\"throttler-0\"},\"host\":\"ip-10-11-108-110.us-east-2.compute.internal\",\"container_name\":\"throttler\",\"docker_id\":\"90015d1ac334d32e38f69fa603b22b02a03182d00df7264a006f29b1f4fa066a\",\"container_hash\":\"529726762838.dkr.ecr.eu-west-1.amazonaws.com\\/throttler@sha256:1f8859d7e841f7f22d24bc882f6677199a747c0b40ee2f2ccb1cc4e10f46d4d3\",\"container_image\":\"529726762838.dkr.ecr.eu-west-1.amazonaws.com\\/throttler:392e6bdc\"},\"stream\":\"stdout\",\"log\":\"[INFO] 2022-11-03T07:03:02,464 com.coralogix.throttler.model.Violation throttler-0af20f87-e6c8-4b51-8993-9e726245860a-StreamThread-1 Company transitioned into violated state. Company: [2010806]. Rate: [1298828.7833333334B\\/s].\",\"time\":\"2022-11-03T07:03:02.464514886Z\"}"; let substitution = "\"message\":\"$message\",\"company\":\"$company\",\"rate\":\"$rate\",${1}"; // result will be a String with the substituted value let result = regex.replace_all(string, substitution); println!("{}", result); }

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 Rust, please visit: https://docs.rs/regex/latest/regex/