简体   繁体   中英

Regular expression and substitution in Python

I have a content string like:

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.
"""

And I want to replace the subhead like 1. 2. 3. and so on, but don't want to affect the actual content numbers like srs-3003.

If I use the following regular expression: re.findall("\d{1}\.", content) result are ['3.', '1.', '2.', '3.', '1.', '2.', '3.'] and '3.' in srs-300 3. will be replaced in content in the next step:

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

How can I proceed?

Your regex is up to the mark. Just in order to not match 3. in srs-3003. you may add ^ anchor. Something like:

^\d+\.

Explanation of the above regex:

  • ^ - Represents start of the line.
  • \d+ - Represents digit class apeearing one or more times.
  • \. - Matches . literally. If you want to remove space also which is in front of every numbered point line; please use + or \s+ .

You can find the demo of the above regex in here.


Sample Implementation in 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)

Please find the sample run of the above program in here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM