V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
amiwrong123
V2EX  ›  程序员

正则匹配点本身时,为啥匹配不到?

  •  
  •   amiwrong123 · Sep 10, 2023 · 676 views
    This topic created in 973 days ago, the information mentioned may be changed or developed.
    import re
    
    result = re.match("[\\.]+[\d]+", "...........................................761")
    if result is None:
        print(result)
    else:
        print("success")
    

    当我执行如上程序时,匹配成功了。

    import re
    
    result = re.match("[\\.]+[\d]+", " ...........................................761")
    if result is None:
        print(result)
    else:
        print("success")
    

    当我执行如上程序时,字符串前面多了个空格,却匹配失败了。

    import re
    
    result = re.match("[\\.]+[\d]+", "first chapter...........................................761")
    if result is None:
        print(result)
    else:
        print("success")
    

    当我执行如上程序时,字符串前面多了个单词,却匹配失败了。

    各位大佬,为啥会匹配失败呀?(正则比较菜,大家轻喷)

    Inn0Vat10n
        1
    Inn0Vat10n  
       Sep 10, 2023
    NoOneNoBody
        2
    NoOneNoBody  
       Sep 10, 2023   ❤️ 1
    match 是从头开始匹配,从中间开始匹配用 re.search
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   867 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 20:47 · PVG 04:47 · LAX 13:47 · JFK 16:47
    ♥ Do have faith in what you're doing.