Regular Expressions 101

Save & Share

  • Save Regex
    ctrl+s
  • Update Regex
    ctrl+⇧+s

Flavor

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

Function

  • Match
  • Substitution
  • List
  • Unit Tests
/
(?x) # free-spacing mode (?(DEFINE) # Within this DEFINE block, we'll define many subroutines # They build on each other like lego until we can define # a "big number" (?<one_to_9> # The basic regex: # one|two|three|four|five|six|seven|eight|nine # We'll use an optimized version: # Option 1: four|eight|(?:fiv|(?:ni|o)n)e|t(?:wo|hree)| # s(?:ix|even) # Option 2: (?:f(?:ive|our)|s(?:even|ix)|t(?:hree|wo)|(?:ni|o)ne|eight) ) # end one_to_9 definition (?<ten_to_19> # The basic regex: # ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen| # eighteen|nineteen # We'll use an optimized version: # Option 1: twelve|(?:(?:elev|t)e|(?:fif|eigh|nine|(?:thi|fou)r| # s(?:ix|even))tee)n # Option 2: (?:(?:(?:s(?:even|ix)|f(?:our|if)|nine)te|e(?:ighte|lev))en| t(?:(?:hirte)?en|welve)) ) # end ten_to_19 definition (?<two_digit_prefix> # The basic regex: # twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety # We'll use an optimized version: # Option 1: (?:fif|six|eigh|nine|(?:tw|sev)en|(?:thi|fo)r)ty # Option 2: (?:s(?:even|ix)|t(?:hir|wen)|f(?:if|or)|eigh|nine)ty ) # end two_digit_prefix definition (?<one_to_99> (?&two_digit_prefix)(?:[- ](?&one_to_9))?|(?&ten_to_19)| (?&one_to_9) ) # end one_to_99 definition (?<one_to_999> (?&one_to_9)[ ]hundred(?:[ ](?:and[ ])?(?&one_to_99))?| (?&one_to_99) ) # end one_to_999 definition (?<one_to_999_999> (?&one_to_999)[ ]thousand(?:[ ](?&one_to_999))?| (?&one_to_999) ) # end one_to_999_999 definition (?<one_to_999_999_999> (?&one_to_999)[ ]million(?:[ ](?&one_to_999_999))?| (?&one_to_999_999) ) # end one_to_999_999_999 definition (?<one_to_999_999_999_999> (?&one_to_999)[ ]billion(?:[ ](?&one_to_999_999_999))?| (?&one_to_999_999_999) ) # end one_to_999_999_999_999 definition (?<one_to_999_999_999_999_999> (?&one_to_999)[ ]trillion(?:[ ](?&one_to_999_999_999_999))?| (?&one_to_999_999_999_999) ) # end one_to_999_999_999_999_999 definition (?<bignumber> zero|(?&one_to_999_999_999_999_999) ) # end bignumber definition (?<zero_to_9> (?&one_to_9)|zero ) # end zero to 9 definition (?<decimals> point(?:[ ](?&zero_to_9))+ ) # end decimals definition ) # End DEFINE ####### The Regex Matching Starts Here ######## ^(?&bignumber)(?:[ ](?&decimals))?$ ### Other examples of groups we could match ### #(?&bignumber) # (?&one_to_99) # (?&one_to_999) # (?&one_to_999_999) # (?&one_to_999_999_999) # (?&one_to_999_999_999_999) # (?&one_to_999_999_999_999_999)
/
gm
(?x)
match the remainder of the pattern with the following effective flags: gmx
x modifier: extended. Spaces and text after a # in the pattern are ignored
# free-spacing mode
free-spacing mode
Comment: free-spacing mode
Subpattern definition construct
(?(DEFINE) # Within this DEFINE block, we'll define many subroutines # They build on each other like lego until we can define # a "big number" (?<one_to_9> # The basic regex: # one|two|three|four|five|six|seven|eight|nine # We'll use an optimized version: # Option 1: four|eight|(?:fiv|(?:ni|o)n)e|t(?:wo|hree)| # s(?:ix|even) # Option 2: (?:f(?:ive|our)|s(?:even|ix)|t(?:hree|wo)|(?:ni|o)ne|eight) ) # end one_to_9 definition (?<ten_to_19> # The basic regex: # ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen| # eighteen|nineteen # We'll use an optimized version: # Option 1: twelve|(?:(?:elev|t)e|(?:fif|eigh|nine|(?:thi|fou)r| # s(?:ix|even))tee)n # Option 2: (?:(?:(?:s(?:even|ix)|f(?:our|if)|nine)te|e(?:ighte|lev))en| t(?:(?:hirte)?en|welve)) ) # end ten_to_19 definition (?<two_digit_prefix> # The basic regex: # twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety # We'll use an optimized version: # Option 1: (?:fif|six|eigh|nine|(?:tw|sev)en|(?:thi|fo)r)ty # Option 2: (?:s(?:even|ix)|t(?:hir|wen)|f(?:if|or)|eigh|nine)ty ) # end two_digit_prefix definition (?<one_to_99> (?&two_digit_prefix)(?:[- ](?&one_to_9))?|(?&ten_to_19)| (?&one_to_9) ) # end one_to_99 definition (?<one_to_999> (?&one_to_9)[ ]hundred(?:[ ](?:and[ ])?(?&one_to_99))?| (?&one_to_99) ) # end one_to_999 definition (?<one_to_999_999> (?&one_to_999)[ ]thousand(?:[ ](?&one_to_999))?| (?&one_to_999) ) # end one_to_999_999 definition (?<one_to_999_999_999> (?&one_to_999)[ ]million(?:[ ](?&one_to_999_999))?| (?&one_to_999_999) ) # end one_to_999_999_999 definition (?<one_to_999_999_999_999> (?&one_to_999)[ ]billion(?:[ ](?&one_to_999_999_999))?| (?&one_to_999_999_999) ) # end one_to_999_999_999_999 definition (?<one_to_999_999_999_999_999> (?&one_to_999)[ ]trillion(?:[ ](?&one_to_999_999_999_999))?| (?&one_to_999_999_999_999) ) # end one_to_999_999_999_999_999 definition (?<bignumber> zero|(?&one_to_999_999_999_999_999) ) # end bignumber definition (?<zero_to_9> (?&one_to_9)|zero ) # end zero to 9 definition (?<decimals> point(?:[ ](?&zero_to_9))+ ) # end decimals definition )
May only be used to define functions. No matching is done in this group.
# Within this DEFINE block, we'll define many subroutines
Within this DEFINE block, we'll define many subroutines
Comment: Within this DEFINE block, we'll define many subroutines
# They build on each other like lego until we can define
They build on each other like lego until we can define
Comment: They build on each other like lego until we can define
# a "big number"
a "big number"
Comment: a "big number"
Named Capture Group one_to_9
(?<one_to_9> # The basic regex: # one|two|three|four|five|six|seven|eight|nine # We'll use an optimized version: # Option 1: four|eight|(?:fiv|(?:ni|o)n)e|t(?:wo|hree)| # s(?:ix|even) # Option 2: (?:f(?:ive|our)|s(?:even|ix)|t(?:hree|wo)|(?:ni|o)ne|eight) )
# The basic regex:
The basic regex:
Comment: The basic regex:
# one|two|three|four|five|six|seven|eight|nine
one|two|three|four|five|six|seven|eight|nine
Comment: one|two|three|four|five|six|seven|eight|nine
# We'll use an optimized version:
We'll use an optimized version:
Comment: We'll use an optimized version:
# Option 1: four|eight|(?:fiv|(?:ni|o)n)e|t(?:wo|hree)|
Option 1: four|eight|(?:fiv|(?:ni|o)n)e|t(?:wo|hree)|
Comment: Option 1: four|eight|(?:fiv|(?:ni|o)n)e|t(?:wo|hree)|
# s(?:ix|even)
s(?:ix|even)
Comment: s(?:ix|even)
# Option 2:
Option 2:
Comment: Option 2:
Non-capturing group
(?:f(?:ive|our)|s(?:even|ix)|t(?:hree|wo)|(?:ni|o)ne|eight)
# end one_to_9 definition
end one_to_9 definition
Comment: end one_to_9 definition
Named Capture Group ten_to_19
(?<ten_to_19> # The basic regex: # ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen| # eighteen|nineteen # We'll use an optimized version: # Option 1: twelve|(?:(?:elev|t)e|(?:fif|eigh|nine|(?:thi|fou)r| # s(?:ix|even))tee)n # Option 2: (?:(?:(?:s(?:even|ix)|f(?:our|if)|nine)te|e(?:ighte|lev))en| t(?:(?:hirte)?en|welve)) )
# The basic regex:
The basic regex:
Comment: The basic regex:
# ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|
ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|
Comment: ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|
# eighteen|nineteen
eighteen|nineteen
Comment: eighteen|nineteen
# We'll use an optimized version:
We'll use an optimized version:
Comment: We'll use an optimized version:
# Option 1: twelve|(?:(?:elev|t)e|(?:fif|eigh|nine|(?:thi|fou)r|
Option 1: twelve|(?:(?:elev|t)e|(?:fif|eigh|nine|(?:thi|fou)r|
# s(?:ix|even))tee)n
s(?:ix|even))tee)n
# Option 2:
Option 2:
Non-capturing group
(?:(?:(?:s(?:even|ix)|f(?:our|if)|nine)te|e(?:ighte|lev))en| t(?:(?:hirte)?en|welve))
# end ten_to_19 definition
end ten_to_19 definition
Comment: end ten_to_19 definition
Named Capture Group two_digit_prefix
(?<two_digit_prefix> # The basic regex: # twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety # We'll use an optimized version: # Option 1: (?:fif|six|eigh|nine|(?:tw|sev)en|(?:thi|fo)r)ty # Option 2: (?:s(?:even|ix)|t(?:hir|wen)|f(?:if|or)|eigh|nine)ty )
# The basic regex:
The basic regex:
Comment: The basic regex:
# twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety
twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety
Comment: twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety
# We'll use an optimized version:
We'll use an optimized version:
# Option 1: (?:fif|six|eigh|nine|(?:tw|sev)en|(?:thi|fo)r)ty
Option 1: (?:fif|six|eigh|nine|(?:tw|sev)en|(?:thi|fo)r)ty
# Option 2:
Option 2:
Non-capturing group
(?:s(?:even|ix)|t(?:hir|wen)|f(?:if|or)|eigh|nine)
ty
matches the characters ty literally (case sensitive)
# end two_digit_prefix definition
end two_digit_prefix definition
Comment: end two_digit_prefix definition
Named Capture Group one_to_99
(?<one_to_99> (?&two_digit_prefix)(?:[- ](?&one_to_9))?|(?&ten_to_19)| (?&one_to_9) )
# end one_to_99 definition
end one_to_99 definition
Named Capture Group one_to_999
(?<one_to_999> (?&one_to_9)[ ]hundred(?:[ ](?:and[ ])?(?&one_to_99))?| (?&one_to_99) )
# end one_to_999 definition
end one_to_999 definition
Named Capture Group one_to_999_999
(?<one_to_999_999> (?&one_to_999)[ ]thousand(?:[ ](?&one_to_999))?| (?&one_to_999) )
# end one_to_999_999 definition
end one_to_999_999 definition
Named Capture Group one_to_999_999_999
(?<one_to_999_999_999> (?&one_to_999)[ ]million(?:[ ](?&one_to_999_999))?| (?&one_to_999_999) )
# end one_to_999_999_999 definition
end one_to_999_999_999 definition
Named Capture Group one_to_999_999_999_999
(?<one_to_999_999_999_999> (?&one_to_999)[ ]billion(?:[ ](?&one_to_999_999_999))?| (?&one_to_999_999_999) )
# end one_to_999_999_999_999 definition
end one_to_999_999_999_999 definition
Named Capture Group one_to_999_999_999_999_999
(?<one_to_999_999_999_999_999> (?&one_to_999)[ ]trillion(?:[ ](?&one_to_999_999_999_999))?| (?&one_to_999_999_999_999) )
# end one_to_999_999_999_999_999 definition
end one_to_999_999_999_999_999 definition
Named Capture Group bignumber
(?<bignumber> zero|(?&one_to_999_999_999_999_999) )
# end bignumber definition
end bignumber definition
Named Capture Group zero_to_9
(?<zero_to_9> (?&one_to_9)|zero )
# end zero to 9 definition
end zero to 9 definition
Named Capture Group decimals
(?<decimals> point(?:[ ](?&zero_to_9))+ )
# end decimals definition
end decimals definition
# End DEFINE
End DEFINE
Comment: End DEFINE
####### The Regex Matching Starts Here ########
###### The Regex Matching Starts Here ########
Comment: ###### The Regex Matching Starts Here ########
^ asserts position at start of a line
(?&bignumber) recurses the subpattern named bignumber
Non-capturing group
(?:[ ](?&decimals))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[ ]
matches the character with index 3210 (2016 or 408) literally (case sensitive)
(?&decimals) recurses the subpattern named decimals
$ asserts position at the end of a line
### Other examples of groups we could match ###
## Other examples of groups we could match ###
Comment: ## Other examples of groups we could match ###
#(?&bignumber)
(?&bignumber)
Comment: (?&bignumber)
# (?&one_to_99)
(?&one_to_99)
Comment: (?&one_to_99)
# (?&one_to_999)
(?&one_to_999)
Comment: (?&one_to_999)
# (?&one_to_999_999)
(?&one_to_999_999)
Comment: (?&one_to_999_999)
# (?&one_to_999_999_999)
(?&one_to_999_999_999)
# (?&one_to_999_999_999_999)
(?&one_to_999_999_999_999)
Comment: (?&one_to_999_999_999_999_999)
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.Try launching the debugger to find out why.

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php