问题描述
我正在寻找与Tcl的操作等效的Python。 具体来说,我想正确处理特殊序列(* 、?和[chars])。
例如,给定三个Python字符串:
expected = 'Foo? Bar* Tar'
actual1 = 'Foo2 Barfluff Tar'
actual2 = 'Foo Bar Tar'
匹配操作match(expected,actual1)
应该返回true,但是match(expected,actual2)
应该返回false。
非常感谢!
1楼
您需要 。
尽管re
提供了完整的功能强大的正则表达式,但fnmatch
会执行您要查找的有限的,外壳样式的通配符匹配。
对于区分大小写的匹配,它很简单:
>>> fnmatch.fnmatchcase(actual1, expected)
True
>>> fnmatch.fnmatchcase(actual2, expected)
False
如果要遵循操作系统的区分大小写规则(即在Windows上不区分大小写,在大多数其他操作系统上敏感),则可以使用普通的fnmatch.fnmatch
来调用自动区分大小写。