代码之家  ›  专栏  ›  技术社区  ›  Miro Samek

Python相当于Tcl的“字符串匹配”

  •  0
  • Miro Samek  · 技术社区  · 8 年前

    string match 操作。具体来说,我想正确处理特殊序列(*、?、和[chars])。

    expected = 'Foo? Bar* Tar'
    actual1 = 'Foo2 Barfluff Tar'
    actual2 = 'Foo Bar Tar'
    

    匹配操作 match(expected,actual1) 应该返回true,但是 match(expected,actual2)

    谢谢!

    1 回复  |  直到 8 年前
        1
  •  3
  •   ShadowRanger    8 年前

    你想要什么 the fnmatch module . 而 re FN匹配

    >>> fnmatch.fnmatchcase(actual1, expected)
    True
    >>> fnmatch.fnmatchcase(actual2, expected)
    False
    

    fnmatch.fnmatch 调用自动大小写规范化。