Difference between regex_match and regex_search?

前端 未结 4 1974
渐次进展
渐次进展 2020-12-06 09:02

I was experimenting with regular expression in trying to make an answer to this question, and found that while regex_match finds a match, regex_search

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 09:42

    Your regex works fine (both match, which is correct) in VS 2012rc.

    In g++ 4.7.1 (-std=gnu++11), if using:

    • ".*FILE_(.+)_EVENT\\.DAT.*", regex_match matches, but regex_search doesn't.
    • ".*?FILE_(.+?)_EVENT\\.DAT.*", neither regex_match nor regex_search matches (O_o).

    All variants should match but some don't (for reasons that have been pointed out already by betabandido). In g++ 4.6.3 (-std=gnu++0x), the behavior is identical to g++ 4.7.1.

    Boost (1.50) matches everything correctly w/both pattern varieties.

    Summary:

                            regex_match      regex_search
     -----------------------------------------------------
     g++ 4.6.3 linux            OK/-               -
     g++ 4.7.1 linux            OK/-               -
     vs 2010                     OK                OK
     vs 2012rc                   OK                OK
     boost 1.50 win              OK                OK
     boost 1.50 linux            OK                OK
     -----------------------------------------------------
    

    Regarding your pattern, if you mean a dot character '.', then you should write so ("\\."). You can also reduce backtracking by using non-greedy modifiers (?):

    ".*?FILE_(.+?)_EVENT\\.DAT.*"
    

提交回复
热议问题