【问题标题】:How to remove text between two specfic words in python如何在python中删除两个特定单词之间的文本
【发布时间】:2019-08-02 07:48:43
【问题描述】:

我已经使用漂亮的汤包解析了一个 url 以获取它的文本。我想删除条款和条件部分中的所有文本,即“关键条款:...... T&Cs apply”段落中的所有单词。

以下是我尝试过的:

import re

#"text" is part of the text contained in the url
text="Welcome to Company Key.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
Key Terms; Single bets only. Any returns from the free bet will be paid 
back into your account minus the free bet stake. Free bets can only be 
placed at maximum odds of 5.00 (4/1). Bonus will expire midnight, Tuesday 
26th February 2019. Bonus T&Cs and General T&Cs apply.                                                                                                                                                                                                                                                    
"
rex=re.compile('Key\ (.*?)T&Cs.')"""to remove words between "Key" and 
"T&Cs" """
terms_and_cons=rex.findall(text)
text=re.sub("|".join(terms_and_cons)," ",text)
#I also tried: text=re.sub(terms_and_cons[0]," ",text)
print(text)

上面只是保持字符串'text'不变,即使列表“terms_and_cons”是非空的。如何成功删除“Key”和“T&Cs”之间的单词?请帮我。很长一段时间以来,我一直被困在这段据说很简单的代码上,这真的令人沮丧。谢谢。

【问题讨论】:

  • 你可以在你的正则表达式的开头添加一个 ^ 符号来否定前瞻,这样 term n cons 变量只会得到没有被正则表达式过滤的东西吗?

标签: python regex analysis


【解决方案1】:

您在正则表达式中缺少 re.DOTALL 标志,以匹配换行符和点。

方法一:使用re.sub

import re

text="""Welcome to Company Key.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
Key Terms; Single bets only. Any returns from the free bet will be paid 
back into your account minus the free bet stake. Free bets can only be 
placed at maximum odds of 5.00 (4/1). Bonus will expire midnight, Tuesday 
26th February 2019. Bonus T&Cs and General T&Cs apply.                                                                                                                                                                                                                                                    
"""

rex = re.compile("Key\s(.*)T&Cs", re.DOTALL)
text = rex.sub("Key T&Cs", text)
print(text)

方法二:使用组

将文本与组匹配,并从原始文本中删除该组的文本。

import re

text="""Welcome to Company Key.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
Key Terms; Single bets only. Any returns from the free bet will be paid 
back into your account minus the free bet stake. Free bets can only be 
placed at maximum odds of 5.00 (4/1). Bonus will expire midnight, Tuesday 
26th February 2019. Bonus T&Cs and General T&Cs apply.                                                                                                                                                                                                                                                    
"""

rex = re.compile("Key\s(.*)T&Cs", re.DOTALL)
matches = re.search(rex, text)
text = text.replace(matches.group(1), "")
print(text)

【讨论】:

  • 感谢您的帮助。如果我有两段,每段都写有条款和条件,而另一段之间有有用的文字,该怎么办。在这种情况下,您的代码也会删除它们之间的段落。如何解决这个问题?
  • 您可以创建两个捕获组:"Key\s(.*)WORD(.*)&Cs" 其中 WORD 是有用文本开始之前的某个正则表达式。然后你可以像方法 2 一样使用 goup(1) 和 group(2) 进行两次替换
猜你喜欢
  • 2021-11-27
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
  • 2022-11-25
  • 2021-05-14
  • 2019-09-14
相关资源
最近更新 更多