How to turn on/off greedy-ness in clojure re-patterns?
(re-find #"(.+)-(.+)" "hello-world-you") => ["hello-world-you" "hello-world" "you"]
vs
(re-find #"(.+)-(.+)" "hello-world-you") => ["hello-world-you" "hello" "world-you"]
You make it non-greedy by using ". *?" When using the latter construct, the regex engine will, at every step it matches text into the "." attempt to match whatever make come after the ". *?" . This means that if for instance nothing comes after the ".
A non-greedy match means that the regex engine matches as few characters as possible—so that it still can match the pattern in the given string. For example, the regex 'a+?' will match as few 'a' s as possible in your string 'aaaa' . Thus, it matches the first character 'a' and is done with it.
About Non-Greedy Search The Non-Greedy search makes it possible to identify the target element from a pool of similar applications, matching the attributes you specify. It needs to be included in the top-level tag of a selector. If a generated selector contains the idx attribute, its value is set by default to * .
The ?
makes quantifiers, such as +
, non-greedy. By default, they are greedy.
(.+)
(.+?)
By the way, this is just the direct, simple, and to-the-point answer. @fge's answer suggests the better way of doing it. Check it out for future expressions.
Don't use .+
, use a complemented character class: this avoids having to care about greediness at all.
You should have used this as a regex: ([^-]+)-([^-]+)
.
Always make the effort to qualify your input as well as possible. Here you wanted to match everything which is not a dash, once or more, and capture it (([^-]+)
), then a dash (-
), then (again) everything which is not a dash, once or more, and capture it (([^-]+)
).
Relying on quantifiers' (non-)greediness is a fundamental error if you know you can describe your input without relying on it. Not only it is a source of error (as you yourself demonstrate), it is also a hindrance for the regex engine to perform at its maximum efficiency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With