繁体   English   中英

正则表达式:在特定单词之前和之后查找一些

[英]Regex: finding some before and after specific word

我试图找到包含单词的任何内容。 到目前为止,我有(r'>(.*(word)?)</a>'但我遇到了一些麻烦。我想要的是有some text (word) some text

使用re.findall

re.findall('(.+)\(word\)(.+)','some text (word) some text')

给出:

[('some text ', ' some text')]

一个例子:

#1
>>> text = 'wow! python (word) is amazing!' #case with "word" in parenthesis. 
>>> re.findall('(.+)\(word\)(.+)',text)
[('wow! python ', ' is amazing!')]
#2
>>> text = 'wow! python the is amazing!'  #case without "word" in parenthesis. 
>>> re.findall('(.+)the(.+)',text)
[('wow! python ', ' is amazing!')]

如果您使用:

re.findall(...)[0]

它直接给出:

('some text ', ' some text')
     ↑               ↑
   prefix          suffix

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM