[英]how to use positive and negative look ahead for multiple terms in Python?
我有一個如下所示的數據框
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 units
、 3 times a week
3 times per week
、 1 time/day
3 times per week
。
我正在閱讀關於正面和負面的前后展望。
所以,正在嘗試像下面這樣的東西
[^([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 times a day
、 3 time/wk
、 3 times per day
3 times a month
3 times/month
。
基本上,我希望我的輸出類似於以下內容(刪除諸如 xx 個單位、每天 xx 次、每周 xx 次、xx 時間/天、xx 時間/周、xx 時間/周、每周 xx 次等術語)
你可以考慮一個模式
\s*\d+\s*(?:units?|times?(?:\s+(?:a|per)\s+|\s*/\s*)(?:d(?:ay)?|w(?:ee)?k|month|y(?:ea)?r?))
查看正則表達式演示
注意: \\d+
匹配一位或多位數字。 如果您需要匹配任何數字,請考慮以您期望的格式為數字使用其他模式,請參閱正則表達式以查找小數/浮點數? , 例如。
圖案詳情
\\s*
- 零個或多個空白字符\\d+
- 一位或多位數字\\s*
- 零個或多個空格(?:units?|times?(?:\\s+(?:a|per)\\s+|\\s*/\\s*)(?:d(?:ay)?|w(?:ee)?k|month|y(?:ea)?r?))
- 非捕獲組匹配:
units?
- unit
或多個units
|
- 或者times?
- time
或times
(?:\\s+(?:a|per)\\s+|\\s*/\\s*)
- a
或per
用 1+ 個空格包圍,或/
用 0+ 個空格包圍(?:d(?:ay)?|w(?:ee)?k|month|y(?:ea)?r?)
- d
或day
,或wk
或week
,或month
,或y
/ yea
/ yr
如果您只需要匹配整個單詞,請使用單詞邊界\\b
:
\s*\b\d+\s*(?:units?|times?(?:\s+(?:a|per)\s+|\s*/\s*)(?:d(?:ay)?|w(?:ee)?k|month|y(?:ea)?r?))\b
在熊貓中,使用
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', '')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.