我有这样一根线:
'Medicine Treatment: Not applicable'
现在,我想匹配相应的治疗,但治疗前没有空格。
所以我只想匹配:"Not applicable"
而不是" Not applicable"
我很肯定是这样的:
(?<=Medicine Treatment:)[^\s].*
或(?<=Medicine Treatment:)\S.*
(?<=Medicine Treatment:).*
只返回我不想要的:" Not applicable"
Python} module 也可以)。因此,您不能使用
re
不支持匹配未知长度字符串的模式(尽管PyPi ^{re.search(r'(?<=Medicine Treatment:\s*).*', txt)
(它可以与PyPiregex.search
一起使用)您可以使用捕获组而不是查找组:
参见Python demo
细节
Medicine Treatment:
-文本字符串(用作所需匹配的左侧上下文)\s*
-使用零个或多个空格字符(.*)
-尽可能多地将除换行符以外的任何0个或更多字符捕获到组1中李>您可以不使用regex,而是通过组合
split()
和strip()
函数来实现这一点:输出:
如果您有一个字符串
s
,s.strip()
将是该字符串的副本,并且s.lstrip()
将只删除左边的空白相关问题 更多 >
编程相关推荐