2017-08-16 88 views
0

我在使用我的用户界面时遇到了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域’?

+0

单引号和空格不是特殊的正则表达式字符。你可以创建一个[mcve]来显示你确切的问题? –

+2

是否有任何理由使用're.search(word_check1,text)'而不是简单的'word_check1 in text'? – Andersson

+0

可能重复[如何在python中跳过(反斜杠和单引号)或双引号!](https://stackoverflow.com/questions/6717435/how-to-escape-a-backslash-anda-a-单引号或双引号在蟒蛇) – JeffC

回答

1

你试过了转义字符'\'吗? https://docs.python.org/2.0/ref/strings.html

>>> word_check2 = "Senders\' Domains" 
>>> print (word_check2) 
Senders' Domains 
>>> 
+1

不会在你的情况下逃脱工作,无论? – Mangohero1

+0

谢谢你,这工作,并帮助我匹配找到的文本。 @ mangoHero1也是正确的我再次尝试没有逃脱,它的工作,所以这部分是我误解re.search如何工作的问题。 – Golshy

+0

好吧,很高兴它的工作。 - 完全根据你的问题猜测。我是新人,现在只能添加评论,只能“回答”我认为很愚蠢的东西。 –