I have some strings that are made up of different characters (in variable quantity) and end with & (like someText&
or someOtherText&
). I need to match each of them (separately) in their entirety, putting the & (if found) in a group. There can be any kind of character before the &.
I tried .+(&)?
and the strings did match, but the .+
part matched the entire strings and & never ended up in the group.
Why isn't .+(&)?
working?
Asked
Active
Viewed 10 times
-1
![](../../users/profiles/16039311.webp)
user288668
- 1
- 1
-
`.+` is too generic and greedy. Use `\w+(&)?`, for example (to match only letters/digits/underscores before a potential `&`). Or `[^&]+(&)?` to match any one or more chars other than a `&` before an eventual `&`. What about whitespace? If there can be no whitespace in the tokens, use ``[^&\s]+(&)?``. – Wiktor Stribiżew May 26 '21 at 15:57
-
Thanks for you answer. I already solved the problem with `[^&]+(&)?`. What I wanted to know is why it doesn't work with `.+(&)?`. I thought that `.+` would match the entire string but `(&)?` would make it backtrack to match the & when it's there and put it in a group. – user288668 May 26 '21 at 16:02
-
No, `(&)?` at the end of pattern would never cause backtracking since it matches at any position in the string as it can match an empty string. – Wiktor Stribiżew May 26 '21 at 16:03
-
Ah, I see. Thanks. – user288668 May 26 '21 at 16:10
-
To clarify a bit more: patterns with variable width quantifiers cause backtracking only when followed with obligatory patterns, not when they are at the end of a regex pattern. – Wiktor Stribiżew May 26 '21 at 16:21
-
1@WiktorStribiżew Thanks. – user288668 May 26 '21 at 22:50