Is there any real use for reluctance? "in regular expressions?

Insufficient quantifiers can be very useful. However, I can not come up with any use ??, reluctant version ?. Can someone give me an example, or is it just because other quantifiers also have reluctant versions?

+3
source share
3 answers

Here is an example of a regular expression using a quantifier ??:

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

It matches every word that either follows a hyphen or precedes a hyphen. In foo-barit will correspond to fooand -barnot foo-and barhow (?:\w+-?|-\w+).

+3
source

- , , ( #):

Match username = Regex.Match(input, @"^([\w\W]*?)(@[\w\W]+\.[\w\W])??$");

, , , , , @. , , , , @domain.com .

: , , - , . , ...;)

+1

Perhaps, perhaps, if you want something in a different capture group than otherwise. I cannot remember having ever seen him, and I cannot come up with an example that would not be very far-fetched.

0
source

Source: https://habr.com/ru/post/1709370/


All Articles