std::regex_match and lazy quantifier with strange behavior

后端 未结 1 853
渐次进展
渐次进展 2021-01-26 11:13

I know that:
Lazy quantifier matches: As Few As Possible (shortest match)

Also know that the constructor:

basic_regex( ...,
             


        
相关标签:
1条回答
  • 2021-01-26 12:00

    I don't see any inconsistency. regex_match tries to match the whole string, so s?/.+?/g? lazily expands till the whole string is covered.

    These "diagrams" (for regex_search) will hopefully help to get the idea of greediness:

    Non-greedy:
    
    a.*?a: ababa
    a|.*?a: a|baba
    a.*?|a: a|baba  # ok, let's try .*? == "" first
    # can't go further, backtracking
    a.*?|a: ab|aba  # lets try .*? == "b" now
    a.*?a|: aba|ba
    # If the regex were a.*?a$, there would be two extra backtracking
    # steps such that .*? == "bab".
    
    Greedy:
    
    a.*?a: ababa
    a|.*a: a|baba
    a.*|a: ababa|  # try .* == "baba" first
    # backtrack
    a.*|a: abab|a  # try .* == "bab" now
    a.*a|: ababa|
    

    And regex_match( abc ) is like regex_search( ^abc$ ) in this case.

    0 讨论(0)
提交回复
热议问题