0

The documentation for Rust regexes claims the following:

Notice the use of the ^ and $ anchors. In this crate, every expression is executed with an implicit .*? at the beginning and end, which allows it to match anywhere in the text. Anchors can be used to ensure that the full text matches an expression.

What does this mean exactly? I get that having .*? at the beginning and end means that if there is a match in the text, it will match no matter where it is (which is not what I want when constructing a lexer), but how do the anchors ^ and $ fit into all this?

Does placing a ^ at the start of the string allow me to only search at the very beginning of the string? How about $, does it match only at the very end of the input? In other words, does placing these anchors disable the implicit .*?s?

sesodesa
  • 1,473
  • 2
  • 15
  • 24
  • In other words it's implying that matching `a` is similar to a Unix globbing `*a*`. – revo May 29 '20 at 17:12
  • @revo And how do I disable the globbing? I got that is what it does, but I do not wish to do so. – sesodesa May 29 '20 at 17:13
  • If you want to match `a` only and not `ab` or any other occurrences of `a` then try those anchors: `^a$` – revo May 29 '20 at 17:18
  • `^` matches the start of input. `$` matches the end of input. The docs are not negating that, you are misinterpreting the docs that clearly say *"Anchors can be used to ensure that the full text matches an expression."* – Wiktor Stribiżew May 29 '20 at 17:36

0 Answers0