python - 如何在Python中对多个术语使用正向和负向前瞻?

标签 python python-3.x regex pandas dataframe

我有一个如下所示的数据框

df = pd.DataFrame({'person_id': [11,11,11,11,11,11,11,11,11,11],
                   'text':['inJECTable 1234 Eprex DOSE 4000 units on NONd',
                           'department 6789 DOSE 8000 units on DIALYSIS days  -  IV Interm',
                           'inJECTable 4321 Eprex DOSE - 3 times/wk on NONdialysis day',
                           'insulin MixTARD  30/70 - inJECTable 46 units',
                           'insulin ISOPHANE -- InsulaTARD  Vial -  inJECTable 56 units  SC SubCutaneous',
                           '1-alfacalcidol DOSE 1 mcg  - 3 times a week  -  IV Intermittent',
                           'jevity liquid - FEEDS PO  Jevity  -  237 mL  -  1 times per day',
                           '1-alfacalcidol DOSE 1 mcg  - 3 times per week  -  IV Intermittent',
                           '1-supported DOSE 1 mcg  - 1 time/day  -  IV Intermittent',
                           '1-testpackage DOSE 1 mcg  - 1 time a day  -  IV Intermittent']})

我想删除遵循以下模式的单词/字符串:46 单位每周 3 次每周 3 次1次/天

我正在阅读有关积极和消极的 future 展望。

所以,正在尝试像下面这样的事情

[^([0-9\s]*(?=units))]  #to remove terms like `46 units` from the string
[^[0-9\s]*(?=times)(times a day)] # don't know how to make this work for all time variants

时间变量例如:每天 3 次每周 3 次每天 3 次每月 3 次3 次/月

基本上,我希望我的输出如下所示(删除诸如 xx 单位、每天 xx 时间、每周 xx 次、xx 时间/天、xx 时间/周、xx 时间/周、xx 次每周等术语等)

enter image description here

最佳答案

您可以考虑这样的模式

\s*\d+\s*(?:units?|times?(?:\s+(?:a|per)\s+|\s*/\s*)(?:d(?:ay)?|w(?:ee)?k|month|y(?:ea)?r?))

请参阅regex demo

注意:\d+ 匹配一个或多个数字。如果您需要匹配任何数字,请考虑对您期望的格式的数字使用其他模式,请参阅 regular expression for finding decimal/float numbers? ,例如。

图案详细信息

  • \s* - 零个或多个空白字符
  • \d+ - 一位或多位数字
  • \s* - 零个或多个空格
  • (?:单位?|次?(?:\s+(?:a|per)\s+|\s*/\s*)(?:d(?:ay)?|w( ?:ee)?k|month|y(?:ea)?r?)) - 非捕获组匹配:
    • 单位? - 单位单位
    • | - 或
    • 次? - 时间
    • (?:\s+(?:a|per)\s+|\s*/\s*) - aper 用 1 个以上空格括起来,或 / 用 0 个以上空格括起来
    • (?:d(?:ay)?|w(?:ee)?k|月|y(?:ea)?r?) - d,或,或,或y//

如果您只需要匹配整个单词,请使用单词边界,\b:

\s*\b\d+\s*(?:units?|times?(?:\s+(?:a|per)\s+|\s*/\s*)(?:d(?:ay)?|w(?:ee)?k|month|y(?:ea)?r?))\b

在 Pandas 中,使用

df['text'] = df['text'].str.replace(r'\s*\b\d+\s*(?:units?|times?(?:\s+(?:a|per)\s+|\s*/\s*)(?:d(?:ay)?|w(?:ee)?k|month|y(?:ea)?r?))\b', '')

关于python - 如何在Python中对多个术语使用正向和负向前瞻?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64157077/

相关文章:

python - Sympy 返回 log 而不是 ln

python - 如何从 Python 中的 YouTube 链接中提取视频 ID?

regex - 正则表达式在Dart中未返回任何匹配项,但可用于在线正则表达式测试器

python - 如何在 Python 中创建守护进程?

python - SODA API 未返回数据集中的所有字段

python - 目录存在时 Shutil.copy IO Error2

python - 如何获取单引号内的字符串但忽略 "' s "and " 't "?

python - 如何在pydantic中更改日期格式

arrays - Numpy 在所有元素之间做乘积,然后插入到一个三角形的二维数组中

regex - 正则表达式中的单词边界是什么?