txt: ## regexes.md
  ## Chunk 5 of 5
   /:ratchet [ab | abc]:! cd/;  # OUTPUT: «｢abcd｣␤»
  
  ```
  
  ### [Enable frugal backtracking: `:?`](#Regexes "go to top of document")[§](#Enable_frugal_backtracking:_:? "direct link")
  
  The `:?` metacharacter works exactly like `:!`, except that it enables frugal backtracking. It is thus closely related to `?` [frugal quantifier modifier](/language/regexes#Greedy_versus_frugal_quantifiers:_?); again, however `:?` can be used after non-quantifier atoms. This includes contexts in which `?` would be the [zero or one](#Zero_or_one:_?) quantifier (instead of providing backtracking control):
  
  ```raku
  my regex numbers { \d* }
  
  '4247' ~~ /:ratchet <numbers>?  47/;  # OUTPUT: «Nil␤»
  '4247' ~~ /:ratchet <numbers>:? 47/;  # OUTPUT: «｢4247｣␤»
  
  ```
  
  ## [A note on backtracking with sub-regexes](#Regexes "go to top of document")[§](#A_note_on_backtracking_with_sub-regexes "direct link")
  
  `:`, `:!`, and `:?` control the backtracking behavior in their current regex – that is, they cause an atom to behave as though `:ratchet` were set differently in the current regex. However, neither these metacharacters nor `:!ratchet` can cause a non-backtracking sub-regex (including rules or tokens) to backtrack; the sub-regex has already failed to backtrack. On the other hand, they *can* prevent the sub-regex from backtracking. To expand on our previous example:
  
  ```raku
  my regex numbers { \d* }
  
  # By default <numbers> backtracks
  '4247' ~~ / <numbers>  47/;  # OUTPUT: «｢4247｣␤»
  # : can disable backtracking over <numbers>
  '4247' ~~ / <numbers>: 47/;  # OUTPUT: «Nil␤»
  
  my regex numbers-ratchet {:ratchet \d* }
  
  # <numbers-ratchet> never backtracks
  '4247' ~~ /   <numbers-ratchet>   47/;  # OUTPUT: «Nil␤»
  # :! can't make it
  '4247' ~~ /   <numbers-ratchet>:! 47/;  # OUTPUT: «Nil␤»
  # Neither can setting :!ratchet
  '4247' ~~ /:!r <numbers-ratchet>  47/;  # OUTPUT: «Nil␤»
  
  ```
  
  # [`$/` changes each time a regular expression is matched](#Regexes "go to top of document")[§](#$/_changes_each_time_a_regular_expression_is_matched "direct link")
  
  It is worth noting that each time a regular expression is used, the returned [`Match`](/type/Match) (i.e., `$/`) is reset. In other words, `$/` always refers to the very last regular expression matched:
  
  ```raku
  my $answer = 'a lot of Stuff';
  say 'Hit a capital letter!' if $answer ~~ / <[A..Z>]> /;
  say $/;  # OUTPUT: «｢S｣␤»
  say 'hit an x!' if $answer ~~ / x /;
  say $/;  # OUTPUT: «Nil␤»
  
  ```
  
  The reset of `$/` applies independently from the scope where the regular expression is matched:
  
  ```raku
  my $answer = 'a lot of Stuff';
  if $answer ~~ / <[A..Z>]> / {
     say 'Hit a capital letter';
     say $/;  # OUTPUT: «｢S｣␤»
  }
  say $/;     # OUTPUT: «｢S｣␤»
  
  if True {
    say 'hit an x!' if $answer ~~ / x /;
    say $/;   # OUTPUT: «Nil␤»
  }
  
  say $/;     # OUTPUT: «Nil␤»
  
  ```
  
  The very same concept applies to named captures:
  
  ```raku
  my $answer = 'a lot of Stuff';
  if $answer ~~ / $<capital>=<[A..Z>]> / {
     say 'Hit a capital letter';
     say $/<capital>; # OUTPUT: «｢S｣␤»
  }
  
  say $/<capital>;    # OUTPUT: «｢S｣␤»
  say 'hit an x!' if $answer ~~ / $<x>=x /;
  say $/<x>;          # OUTPUT: «Nil␤»
  say $/<capital>;    # OUTPUT: «Nil␤»
  
  ```
  
  # [Best practices and gotchas](#Regexes "go to top of document")[§](#Best_practices_and_gotchas "direct link")
  
  The [Regexes: Best practices and gotchas](/language/regexes-best-practices) provides useful information on how to avoid common pitfalls when writing regexes and grammars.
  