[英]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.