Search code examples
python-3.xregexdata-extraction

Regex to detect multiple repetitions


I have unstructured data where I have to extract BP values as shown below. Right now I have a regex function to extract Bp values. I have a specific case as highlighted in the picture where consecutive values are present and they have to be detected.

Currently, the code I have gives only the 1st value.

I have attached the regex code below.

regex = \b(?:BP:?(?:-Sitting)?|Blood Pressure) \d+/\d+(?: mmHg?)?|B/P - (?:Sys|Dias)tolic \d+|(?:Sys|Dias)tolic Blood Pressure \d+ \w+\b

Any help is really appreciated. Thank you! enter image description here

The current output I am getting is - BP 128/80

Expected Output is - BP 128/80 128/81 128/82 128/83


Solution

  • You can optionally repeat the part that matches the values with the forward slash

    \b(?:BP:?(?:-Sitting)?|Blood Pressure) \d+/\d+(?: \d+/\d+)*(?: mmHg?)?|B/P - (?:Sys|Dias)tolic \d+|(?:Sys|Dias)tolic Blood Pressure \d+ \w+\b
    

    See a regex demo