如何使用python在字符串分区中使用regex



我有一个字符串,如下所示,来自pandas数据帧列

string = "insulin MixTARD  30/70 -  inJECTable 20 unit(s)  SC (SubCutaneous)     -   Hypoglycaemia Protocol if Blood Glucose Level (mmol)  <  4     -   Call Doctor if Blood Glucose Level (mmol)  >  22"

我正在尝试获得如下所示的输出(您可以在返回第二个连字符之前看到所有内容(

insulin MixTARD  30/70 -  inJECTable 20 unit(s)  SC (SubCutaneous)

所以,我尝试了下面的代码

string.partition('   -')[0]  # though this produces the output, not reliable

意思是,我总是想要第二个Hyphen(-(之前的一切。

我不想手动分配空格,而是想写下面这样的内容。不确定以下内容是否正确。你能帮我把第二个连字符之前的东西都记下来吗?

string.partition(r's{2,6}-')[0]

可以帮助我使用partition method and regex获得预期输出吗?

您可以在这里使用re.sub作为一个单行解决方案:

string = "insulin MixTARD  30/70 -  inJECTable 20 unit(s)  SC (SubCutaneous)     -   Hypoglycaemia Protocol if Blood Glucose Level (mmol)  <  4     -   Call Doctor if Blood Glucose Level (mmol)  >  22"
output = re.sub(r'^([^-]+?-[^-]+?)(?=s*-).*$', '\1', string)
print(output)

此打印:

insulin MixTARD  30/70 -  inJECTable 20 unit(s)  SC (SubCutaneous)

正则表达式的解释:

^               from the start of the input
(           capture
[^-]+?  all content up to
-       the first hyphen
[^-]+?  all content up, but not including
)           end capture
(?=s*-)    zero or more whitespace characters followed by the second hyphen
.*          then match the remainder of the input
$               end of the input

尝试使用re.split而不是string.partition:

re.split(r's{2,6}-', string)[0]

使用splitjoin:的简单解决方案

"-".join(string.split("-")[0:2])

最新更新