我在使用我的用户界面时遇到了re.search发现字符串“Senders'Domains”的问题。如何在Python中转义单引号和空格?
word_check1 = 'Senders'
word_check2 = "Senders' Domains"
page_check1, page_check2 = mymessages_page.page_checker(word_check1, word_check2, 'chart-content-container')
当我调试,我page_checker方法查找“发件人//小号域”但随后re.search返回“发件人// sDomains”(去掉空格)。
def page_checker(self, word_check1, word_check2, ids):
container = self.driver.find_element_by_id(ids)
content = container.text
organized_container_content = content.split('\n')
for text in organized_container_content:
if re.search(word_check1, text):
check1 = text
for text2 in organized_container_content:
if re.search(word_check2, text2):
check2 = text2
return check1, check2
break
有没有什么办法可以逃脱单引号(')和空格字符,这样我可以找到并匹配字符串‘发件人我的UI域’?
单引号和空格不是特殊的正则表达式字符。你可以创建一个[mcve]来显示你确切的问题? –
是否有任何理由使用're.search(word_check1,text)'而不是简单的'word_check1 in text'? – Andersson
可能重复[如何在python中跳过(反斜杠和单引号)或双引号!](https://stackoverflow.com/questions/6717435/how-to-escape-a-backslash-anda-a-单引号或双引号在蟒蛇) – JeffC