Questions tagged [reluctant-quantifiers]

Use this tag with Regex that are aware of Greedy, Reluctant and Possessive. If it is just Greedy and Nongreedy then consider using the tag non-greedy.

Reluctant is used more often when the Regex is capable of doing the following three

  • Greedy
  • Reluctant (AKA Nongreedy)
  • Possessive

When the Regex is capable of doing just

  • Greedy
  • Nongreedy

then Nongreedy tends to be used instead of Reluctant

References:

The Java™ Tutorials - Differences Among Greedy, Reluctant, and Possessive Quantifiers - Scroll down to see section.

24 questions
12
votes
6 answers

Regex: Is Lazy Worse?

I have always written regexes like this ([^<]*) but I just learned about this lazy thing and that I can write it like this (.*?) is there any disadvantage to using this…
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
7
votes
1 answer

Difference between exact greedy/reluctant X{n}?

In the documentation for the Java Pattern class, I see that the exact quantifier X{n} has both greedy and reluctant forms: Greedy quantifiers X{n} X, exactly n times ... Reluctant quantifiers X{n}? X, exactly n times ... The documentation…
Owen
  • 38,836
  • 14
  • 95
  • 125
5
votes
2 answers

Writing better regex expression for not using lazy repeat quantifier

I have a regular expression: (]*>))(.*?)() Since it uses lazy repeat quantifier, for longer strings(having options more than 500) it backtracks for more than 100,000 times and fails. Please help me to find a better regular…
shashuec
  • 684
  • 8
  • 20
4
votes
2 answers

Practical use of possessive quantifiers regex

I understand .* (greedy quantifier) backtracks and tries to find a match. and .*+ (possessive quantifier) does not backtrack. However I have been using .* and .\*? frequently but don't know when to use .*+. Can somebody give a situation or an…
Arun Gowda
  • 2,721
  • 5
  • 29
  • 50
4
votes
2 answers

RegExp exercise: reluctant quantifier with a lookahead assertion

Can you explain me how this works? Here is an example: