首页 > 解决方案 > 如何在 python 中比较 "hello:"=="hello"

问题描述

我想忽略字符串中的符号并与非符号字符串进行比较 name="Avengers Endgame"

find_element_by_link_text(name.title()).click()
<a href="#">Avengers: Endgame</a>

标签: pythonselenium

解决方案


您可以在单词类 ( )上使用正则表达式。\W从链接,

\W
    Matches any character which is not a word character. This is the opposite of \w. 
    If the ASCII flag is used this becomes the equivalent of [^a-zA-Z0-9_]. 
    If the LOCALE flag is used, matches characters considered alphanumeric in 
    the current locale and the underscore.

喜欢,

import re
a = 'Avengers Endgame'
b = 'Avengers: Endgame'
if re.sub(r'[\W]', '', a) == re.sub(r'[\W]', '', b):
    print("They match")

推荐阅读