Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8

Function

  • Match
  • Substitution
  • List
  • Unit Tests
`
(?P<mount>rockcut/aws)/(?P<meta>data)?/(?P<env>(dev|test|stage|prod))?/(?P<app>\w+)?/?(?P<team>\w+)?
`
gm
Named Capture Group mount
(?P<mount>rockcut/aws)
rockcut/aws
matches the characters rockcut/aws literally (case sensitive)
/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Named Capture Group meta
(?P<meta>data)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
data
matches the characters data literally (case sensitive)
/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Named Capture Group env
(?P<env>(dev|test|stage|prod))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
4th Capturing Group
(dev|test|stage|prod)
1st Alternative
dev
dev
matches the characters dev literally (case sensitive)
2nd Alternative
test
test
matches the characters test literally (case sensitive)
3rd Alternative
stage
stage
matches the characters stage literally (case sensitive)
4th Alternative
prod
prod
matches the characters prod literally (case sensitive)
/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Named Capture Group app
(?P<app>\w+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
/
matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Named Capture Group team
(?P<team>\w+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Your regular expression does not match the subject string.

Regular Expression
No Match

`
`
gm

Test String

Code Generator

Generated Code

Loading code sample...
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/