我有一个令人尴尬的新手问题,但我想我被困住了,不能直截了当。
我需要一个正则表达式模式,它将在单词'year'或'years'之后添加单词'child'或'children',只有在句子中也存在单词'child'或'children'(我在数据中检测到的模式)。
所以:
“特别适用于一岁或十二岁以下、七岁以下的儿童。”
会在“第一年”之后加上“老”,但在“第二老”之后不加“旧”,也不会在最后两个字之后加上“老”:
“特别是在一岁、或12岁以上的儿童中,有7岁。”
到目前为止,我的模式都是错误的。
if 'child' or 'children' in i.split() and 'old' or 'olds' not in i.split():
i=re.sub(r'year' ,'year old',i)有什么想法吗?谢谢:)
发布于 2019-03-16 13:21:07
有关解释: regex101:https://regex101.com/r/hTsPlF/1,请参阅对https://regex101.com/r/hTsPlF/1的分析。
import re
i = 'Especially in children who are one year or up to twelve years old, for seven years.'
if re.search(r'(\bchild\b)|(\bchildren\b)',i):
re.sub('(years{0,1}) (?!old)',r'\1 old ',i)这意味着:
'Especially in children who are one year old or up to twelve years old, for seven years.'https://stackoverflow.com/questions/55197105
复制相似问题