V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
amiwrong123
V2EX  ›  程序员

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

  •  
  •   amiwrong123 · 2023-09-10 23:40:26 +08:00 · 342 次点击
    这是一个创建于 385 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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  
       2023-09-10 23:43:45 +08:00
    NoOneNoBody
        2
    NoOneNoBody  
       2023-09-10 23:45:17 +08:00   ❤️ 1
    match 是从头开始匹配,从中间开始匹配用 re.search
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2578 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:31 · PVG 08:31 · LAX 17:31 · JFK 20:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.