Regular Expressions 101

@regex101
Donate
Sponsor
Contact
Bug Reports & Feedback
Wiki
Whats new?

Save & Share

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

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python 2.7
  • 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
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.
Comment: Within this DEFINE block, we'll define many subroutines
Comment: They build on each other like lego until we can define
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) )
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)) )
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 )
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) )
Comment: 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) )
Comment: 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) )
Comment: 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) )
Comment: 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) )
Comment: 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) )
Comment: end one_to_999_999_999_999_999 definition
Named Capture Group bignumber
(?<bignumber> zero|(?&one_to_999_999_999_999_999) )
Comment: end bignumber definition
Named Capture Group zero_to_9
(?<zero_to_9> (?&one_to_9)|zero )
Comment: end zero to 9 definition
Named Capture Group decimals
(?<decimals> point(?:[ ](?&zero_to_9))+ )
Comment: end decimals definition
Comment: End DEFINE
Comment: ###### The Regex Matching Starts Here ########
^ asserts position at start of a line
(?&bignumber) recurses the subpattern named bignumber
Non-capturing group
(?:[ ](?&decimals))?
$ asserts position at the end of a line
Comment: ## Other examples of groups we could match ###
Comment: (?&bignumber)
Comment: (?&one_to_99)
Comment: (?&one_to_999)
Comment: (?&one_to_999_999)
Comment: (?&one_to_999_999_999)
Comment: (?&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