簡體   English   中英

Python中的正則表達式和替換

[英]Regular expression and substitution in Python

我有一個內容字符串,如:

content =
"""
the patient monitoring system shall perform a daily device check from 1:30 am to 4:30 am (patient local time). if a device malfunction is detected, the daily device check shall send the malfunction to the clinician. if a patient health alarm is detected, the daily device check shall turn into full interrogation as specified in srs-3003. if no device or patient health issue identified, the daily device check shall end without further notification to the clinicians or patient. if a scheduled interrogation happens on the same day, the daily device check shall be skipped. if any device issue detected during the daily device check, the patient monitoring system shall alarm the patient with red urgent light. . if any patient health issue detected during the daily device check, the patient monitoring system shall alarm the patient with yellow warning light. . if a daily device check fails, it should be retried in 15 minutes up to 3 times. if a daily device check still fails after 3 times, the patient monitoring system shall end the interrogation and notify patient of the failed device check at 8 am that morning. there are 3 types of interrogations as below:
1. scheduled interrogation.
2. daily device check
3. patient initiated interrogation. an interrogation could fail due to the following reasons:
1. failed to establish communication.
2. communication lost.
3. failed to obtain a key data from the implanted device.
"""

我想替換像 1. 2. 3. 這樣的小標題,但不想像 srs-3003 這樣影響實際的內容編號。

如果我使用以下正則表達式: re.findall("\d{1}\.", content)結果是['3.', '1.', '2.', '3.', '1.', '2.', '3.']和 '3.' 在 srs-300 3.將在下一步中替換內容:

num_dot = re.findall("\d+\.", content)
for num in num_dot:
    content = content.replace(num, "")

我該如何進行?

您的正則表達式符合要求。 只是為了不匹配srs-3003.中的3. .。 您可以添加^錨。 就像是:

^\d+\.

上述正則表達式的解釋:

  • ^ - 代表行的開始。
  • \d+ - 代表數字 class 出現一次或多次。
  • \. - 比賽. 字面上地。 如果您還想刪除每個編號點線前面的空間; 請使用+\s+

您可以在此處找到上述正則表達式的演示。


Python 中的示例實現:

import re

regex = r"^\d+\."

test_str = ("the patient monitoring system shall perform a daily device check from 1:30 am to 4:30 am (patient local time). if a device malfunction is detected, the daily device check shall send the malfunction to the clinician. if a patient health alarm is detected, the daily device check shall turn into full interrogation as specified in srs-3003. if no device or patient health issue identified, the daily device check shall end without further notification to the clinicians or patient. if a scheduled interrogation happens on the same day, the daily device check shall be skipped. if any device issue detected during the daily device check, the patient monitoring system shall alarm the patient with red urgent light. . if any patient health issue detected during the daily device check, the patient monitoring system shall alarm the patient with yellow warning light. . if a daily device check fails, it should be retried in 15 minutes up to 3 times. if a daily device check still fails after 3 times, the patient monitoring system shall end the interrogation and notify patient of the failed device check at 8 am that morning. there are 3 types of interrogations as below:\n"
    "1. scheduled interrogation.\n"
    "2. daily device check\n"
    "3. patient initiated interrogation. an interrogation could fail due to the following reasons:\n"
    "1. failed to establish communication.\n"
    "2. communication lost.\n"
    "3. failed to obtain a key data from the implanted device.")

subst = ""

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
    print (result)

請在此處找到上述程序的示例運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM