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
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!
CodePudding user response:
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 )
Matchs
and 1 word chars in group prefix(?<num>s\d )
Capture group num matchs
and 1 digits(?:,(?<num>s\d ))
Repeat 1 times matching,
and captures
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