Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有这两个字符串:
Ultramagnetic MC's
和
Ultramagnetic MC’s <-- the apostrophe is a different char
在 Python 中,但一般来说,我如何编写一个正则表达式来匹配第一个字符串字母和第二个字符串?
我的意思是我只想匹配两个字符串之间的字母并忽略特殊字符,所以我可以匹配Ultramagnetic MCs这样的字符串:
Ultramagnetic MCs
"Ultramagnetic Mc!s"
我猜你正在寻找这样的东西:
import re def equal_letters(x, y): return re.sub(r'\W+', '', x) == re.sub(r'\W+', '', y) >>> equal_letters("Ultramagnetic MC's", "Ultramagnetic MC’s") True >>> equal_letters("Ultramagnetic MC's", "Ultramagnetic Foo") False