美文网首页Python3
Python regex greedy VS non-greed

Python regex greedy VS non-greed

作者: JaedenKil | 来源:发表于2019-01-07 17:06 被阅读11次
    import re
    
    
    a = "100100000001"
    m = re.search(".+1", a)
    print(m.group(0), '\n')
    n = re.search(".+?1", a)
    print(n.group(0))
    
    100100000001 
    
    1001
    

    ?, +?, ??
    The '*', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn’t desired; if the RE <.
    > is matched against '<a> b <c>', it will match the entire string, and not just '<a>'. Adding ? after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using the RE <.*?> will match only '<a>'.
    Refer to here.

    相关文章

      网友评论

        本文标题:Python regex greedy VS non-greed

        本文链接:https://www.haomeiwen.com/subject/uazurqtx.html