如果用户输入变量单词与字符串a中的完全匹配,则我想检索句子的其余部分。
a = 'hello there, I wanted to find out how to split this document'
word = 'wanted'
context = re.search(r'(?=^{user}$)(.*$)'.format(user=word), a)
context.group(0)目前,我已经尝试放置锚和$以确保其匹配,但它将返回此错误消息。
AttributeError: 'NoneType' object has no attribute 'group'当我将下面的代码更改为:
a = 'hello there, I wanted to find out how to split this document'
word = 'wanted'
context = re.search(r'(?={user}$)(.*$)'.format(user=word), a)
context.group(0)任何提示都将不胜感激!谢谢!!
发布于 2019-02-22 07:43:41
没有正则表达式的
a = 'hello there, I wanted to find out how to split this document'
word = 'wanted'
if word in a:
print(a.split(word,1)[1])带有regex的(使用字界):
import re
if re.search(r'\b' + word + r'\b', a):
# print('{0} found'.format(word))
m = re.search("(?<=" + word + ")(.*)", a)
print(m.group(0))
else:
print('{0} not found'.format(word))产出:
to find out how to split this document
to find out how to split this documenthttps://stackoverflow.com/questions/54822266
复制相似问题