Replies: 1 comment
-
Note: We might consider making the breaking change a minor one, since non-greedy quantifiers are not likely to be used as much. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
useNamingConvention
introduced a way to write custom conventions.A user can enforce any style thanks to regexes. The provided regexes are said restricted because we carefully expose the features we want.
A restricted regex is implicitly bounded by the
^
and$
anchors.To make thing clear, I will include these implicit anchors in the following examples.
For now, we support both greedy and non-greedy quantifiers.
In most cases, the choice between greedy and non-greedy quantifiers doesn't change the meaning of a regex because of the implicit anchors.
For example,
^My(.+)Error$
and^My(.+?)Error$
exhibit the same behavior.Unfortunately, in some cases, behavior doesn't match. For example
^(.+)x?$
and^(.+?)x?$
doesn't exhibit the same behavior.If OI take the string
testx
, the first regex captures the entire string, while the second one only capturestest
withoutx
.I think that users always expect non-greediness. Otherwise, this makes
$?
completely useless.Thus, I think we should default to non-greedy quantifiers:
.*
could be non-greedy, instead to be greedy.Unfortunately, the
regex
crate only allows you to swap the greediness behavior usingswap_greed
. This means that.*
becomes non-greedy, while.*?
becomes greedy.This is a breaking change from a user POV.
Another solution is to rely directly on the
regex-automata
crate (that is a dependency of theregex
crate). I have mixed feelings about this option because the crate is still unstable - even if we can expect the crate to be stable enough.regex-automata
allows to use a non-greedy matching strategy using Inout::earliest.Once, we make the non-greedy behavior the default, I think we could deprecate the quantifiers followed by a question mark (
.*?
,.+?
, ...).Beta Was this translation helpful? Give feedback.
All reactions