Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex: Match between delimiters (a letter and a special character) in a string to form new sub-strings

I was working on a certain problem where I have form new sub-strings from a main string.

For e.g.
in_string=ste5ts01,s02,s03

The expected output strings are ste5ts01, ste5ts02, ste5ts03

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There could be comma(,) or forward-slash (/) as the separator and in this case the delimiters are the letter s and ,

The pattern I have created so far:

pattern = r"([^\s,/]+)(?<num>\d+)([,/])(?<num>\d+)(?:\2(?<num>\d+))*(?!\S)"

The issue is, I am not able to figure out how to give the letter ‘s’ as one of the delimiters.

Any help will be much appreciated!

>Solution :

You might use an approach using the PyPi regex module and named capture groups which are available in the captures:

=(?<prefix>s\w+)(?<num>s\d+)(?:,(?<num>s\d+))+

Explanation

  • = Match literally
  • (?<prefix>s\w+) Match s and 1+ word chars in group prefix
  • (?<num>s\d+) Capture group num match s and 1+ digits
  • (?:,(?<num>s\d+))+ Repeat 1+ times matching , and capture s followed by 1+ digits in group num

Example

import regex as re

pattern = r"=(?<prefix>s\w+)(?<num>s\d+)(?:,(?<num>s\d+))+"
s="in_string=ste5ts01,s02,s03"

matches = re.finditer(pattern, s)
for _, m in enumerate(matches, start=1):
    print(','.join([m.group("prefix") + c for c in m.captures("num")]))

Output

ste5ts01,ste5ts02,ste5ts03
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading