我有一个文本段落my_text
,其中包含
........ licensed in Bangladesh. We are happy to announce ......................
................................................
我想提取“#29;孟加拉国"从中。判断我是否想要这个词取决于"许可在"在句子中。
目前的代码如下:
texts = my_text.split("licensed in")
# extract the word before the first dot (.) from texts[1]
在python中执行此操作的更合适的方法是什么?
答案 0 :(得分:2)
这是一个正则表达式的工作:
import re
location = re.search(r"licensed in ([^.]*)", my_text).group(1)
<强>解释强>
licensed\ in\ # Match "licensed in "
( # Match and capture in group 1:
[^.]* # Any number of characters except dots.
) # End of capturing group 1
答案 1 :(得分:0)
怎么样
>>> my_text.split('licensed in ')[1].split('.')[0]
'Bangladesh'