Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any real use for reluctant '?' in regular expressions?

Tags:

regex

Reluctant quantifiers can be very useful. However, I can't think of any use of ??, the reluctant version of ?. Can somebody give me an example, or is it just there because the other quantifiers have reluctant versions too?

like image 795
user114218 Avatar asked Nov 18 '25 02:11

user114218


1 Answers

Here’s an example regular expression that uses the ?? quantifier:

(?:\w+-??|-\w+)

It matches every word that’s either followed by a hyphen or preceeded by a hyphen. In foo-bar it would then match foo and -bar rather than foo- and bar as (?:\w+-?|-\w+) would.

like image 98
Gumbo Avatar answered Nov 21 '25 09:11

Gumbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!