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 (4)

Tools

Sponsors
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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

package main import ( "regexp" "fmt" ) func main() { var re = regexp.MustCompile(`(?m).*\((.|\n)+\)\s*\{`) var str = `# @summary Manages the puppeteer monitor service. # # @param logon_domain # The service will logon using this domain. Specify '.' for a local account. # # @param logon_name # The service will logon using this user name. # # @param logon_password # The service will logon using this password. # # @param ensure # Whether or not install the puppeteermon service. # # @param source # Where to obtain the puppeteermon service executable from. # # @param state # Ensure the puppeteermon service is in the given \`state\`. # # @param startup # How to start the puppeteermon service. # # @param version # The version of the puppeteermon service that should be installed. # class profile::ct::puppeteermon( String $logon_domain = "${hiera('builder_domain', '.')}", String $logon_name = "${hiera('builder_user', 'SYSTEM')}", String $logon_password = "${hiera('builder_passwd', '')}", String $cooldown = '10m', String $min_exec_time = '1m15s', Enum[absent, present] $ensure = present, String $source = 'https://repo.ct.activision.com/repository/puppet/puppeteermon/<%= ::osfamily %>/puppeteermon-<%= version %>', Enum['running', 'stopped'] $state = 'running', Enum['automatic', 'manual', 'disabled'] $startup = 'automatic', String $version = '2.0.11', ) { include profile::ct::puppeteer if $::osfamily == 'windows' { # Non-configurable class parameters $basedir = 'C:/ProgramData/puppeteermon' $os_source = regsubst($source, '<%= ::osfamily %>', 'windows', 'G') $puppeteer_os_source = regsubst($profile::ct::puppeteer::source, '<%= ::osfamily %>', 'windows', 'G') $puppeteer_fetch_source = regsubst($puppeteer_os_source, '<%= version %>', "${profile::ct::puppeteer::version}.exe", 'G') $puppeteer_recovery_source = regsubst($puppeteer_os_source, '<%= version %>', 'recovery.exe', 'G') profile::ct::puppeteer::config { "${profile::ct::puppeteer::basedir}/puppeteer.conf": ensure => $ensure, compass_master_url => $profile::ct::puppeteer::compass_master_url, runinterval => $profile::ct::puppeteer::runinterval, splay => $profile::ct::puppeteer::splay, version => $profile::ct::puppeteer::version, } -> profile::ct::puppeteermon::config { "${basedir}/puppeteermon.conf": ensure => $ensure, cooldown => $cooldown, fetch_url => $puppeteer_fetch_source, download_dir => "${profile::ct::puppeteer::basedir}/fetch", command => "${profile::ct::puppeteer::basedir}/running/puppeteer.exe", } profile::ct::puppeteer::config_new { "${profile::ct::puppeteer::basedir}/puppeteer.yaml": ensure => $ensure, compass_master_url => $profile::ct::puppeteer::compass_master_url, compass_polling_interval => $profile::ct::puppeteer::compass_polling_interval, runinterval => $profile::ct::puppeteer::runinterval, last_run_summary => $profile::ct::puppeteer::last_run_summary, splay => $profile::ct::puppeteer::splay, version => $profile::ct::puppeteer::version, agent_config_platform => $profile::ct::puppeteer::agent_config_platform, } -> profile::ct::puppeteermon::config_new { "${basedir}/puppeteermon.yaml": ensure => $ensure, cooldown => $cooldown, fetch_url => $puppeteer_fetch_source, recovery_url => $puppeteer_recovery_source, download_dir => "${profile::ct::puppeteer::basedir}/stage", command => "${profile::ct::puppeteer::basedir}/running/puppeteer.exe", min_exec_time => $min_exec_time } -> wservice { 'puppeteermon': ensure => $ensure, state => $state, startup => $startup, source => regsubst($os_source, '<%= version %>', "${version}.tar.gz", 'G'), version => $version, path => "${basedir}/puppeteermon.exe", account => "${logon_domain}\\${logon_name}", password => $logon_password, registercmd => ['<%= path %>', 'install'], unregistercmd => ['<%= path %>', 'uninstall'], } if $ensure == present { $enable = $startup ? { 'automatic' => 'true', 'manual' => 'manual', default => 'false' } service { 'puppeteermon': ensure => $state, enable => $enable, require => Wservice['puppeteermon'] } } } elsif $::osfamily == 'Darwin' { # Non-configurable class parameters $basedir = '/opt/puppeteermon' $os_source = regsubst($source, '<%= ::osfamily %>', 'darwin', 'G') $puppeteer_os_source = regsubst($profile::ct::puppeteer::source, '<%= ::osfamily %>', 'darwin', 'G') $puppeteer_fetch_source = regsubst($puppeteer_os_source, '<%= version %>', $profile::ct::puppeteer::version, 'G') $puppeteer_recovery_source = regsubst($puppeteer_os_source, '<%= version %>', 'recovery', 'G') profile::ct::puppeteer::config_new { "${profile::ct::puppeteer::basedir}/puppeteer.yaml": ensure => $ensure, compass_master_url => $profile::ct::puppeteer::compass_master_url, compass_polling_interval => $profile::ct::puppeteer::compass_polling_interval, runinterval => $profile::ct::puppeteer::runinterval, last_run_summary => $profile::ct::puppeteer::last_run_summary, splay => $profile::ct::puppeteer::splay, version => $profile::ct::puppeteer::version, agent_config_platform => $profile::ct::puppeteer::agent_config_platform, } -> profile::ct::puppeteermon::config_new { "${basedir}/puppeteermon.yaml": ensure => $ensure, cooldown => $cooldown, fetch_url => $puppeteer_fetch_source, recovery_url => $puppeteer_recovery_source, download_dir => "${profile::ct::puppeteer::basedir}/stage", command => "${profile::ct::puppeteer::basedir}/running/puppeteer", min_exec_time => $min_exec_time } -> puppeteermon { '/opt/puppeteermon/puppeteermon': ensure => $ensure, version => $version, source => regsubst($os_source, '<%= version %>', $version, 'G'), } if $ensure == present { $enable = $startup ? {'automatic' => 'true', 'manual' => 'manual', default => 'false' } service { 'puppeteermon': ensure => $state, enable => $enable, provider => 'launchd', require => Puppeteermon['/opt/puppeteermon/puppeteermon'] } } } else { # Non-configurable class parameters $basedir = '/opt/puppeteermon' $os_source = regsubst($source, '<%= ::osfamily %>', 'linux', 'G') $puppeteer_os_source = regsubst($profile::ct::puppeteer::source, '<%= ::osfamily %>', 'linux', 'G') $puppeteer_fetch_source = regsubst($puppeteer_os_source, '<%= version %>', $profile::ct::puppeteer::version, 'G') $puppeteer_recovery_source = regsubst($puppeteer_os_source, '<%= version %>', 'recovery', 'G') profile::ct::puppeteer::config_new { "${profile::ct::puppeteer::basedir}/puppeteer.yaml": ensure => $ensure, compass_master_url => $profile::ct::puppeteer::compass_master_url, compass_polling_interval => $profile::ct::puppeteer::compass_polling_interval, runinterval => $profile::ct::puppeteer::runinterval, last_run_summary => $profile::ct::puppeteer::last_run_summary, splay => $profile::ct::puppeteer::splay, version => $profile::ct::puppeteer::version, agent_config_platform => $profile::ct::puppeteer::agent_config_platform, } -> profile::ct::puppeteermon::config_new { "${basedir}/puppeteermon.yaml": ensure => $ensure, cooldown => $cooldown, fetch_url => $puppeteer_fetch_source, recovery_url => $puppeteer_recovery_source, download_dir => "${profile::ct::puppeteer::basedir}/stage", command => "${profile::ct::puppeteer::basedir}/running/puppeteer", min_exec_time => $min_exec_time } -> puppeteermon { '/opt/puppeteermon/puppeteermon': ensure => $ensure, version => $version, source => regsubst($os_source, '<%= version %>', $version, 'G'), } if $ensure == present { $enable = $startup ? {'automatic' => 'true', 'manual' => 'manual', default => 'false' } service { 'puppeteermon': ensure => $state, enable => $enable, provider => 'systemd', require => Puppeteermon['/opt/puppeteermon/puppeteermon'] } } } } ` for i, match := range re.FindAllString(str, -1) { fmt.Println(match, "found at index", 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 Golang, please visit: https://golang.org/pkg/regexp/