【发布时间】:2019-07-26 16:51:04
【问题描述】:
我需要一些关于声明正则表达式的帮助。我的输入如下:
我需要在 regex:python 中提取单词和单词之前并在“_”之间插入 输入
Input
s2 = 'Some other medical terms and stuff diagnosis of R45.2 was entered for this patient. Where did Doctor Who go? Then xxx feea fdsfd'
# my regex pattern
re.sub(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,1}diagnosis", r"\1_", s2)
Desired Output:
s2 = 'Some other medical terms and stuff_diagnosis of R45.2 was entered for this patient. Where did Doctor Who go? Then xxx feea fdsfd'
【问题讨论】:
-
您没有捕获组,但使用
\1引用它。 -
试试
re.sub(r"[^\w'-]+(?=diagnosis)", "_", s2),见regex demo。 -
Wiktor 想说的是必须捕获组,例如:
(?: text not captured ) ( text captured as \1 )+ (text captured as \2)?。见stackoverflow.com/questions/36524507/… -
非常感谢它的工作
-
不会比
str.replace(' diagnosis ','_diagnosis')更好吗?