Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two patterns with named capturing group in Python?

Tags:

python

regex

I have a regular expression which uses the before pattern like so:

>>> RE_SID = re.compile(r'(?P<sid>(?<=sid:)([A-Za-z0-9]+))')
>>> x = RE_SID.search('sid:I118uailfriedx151201005423521">>')
>>> x.group('sid')
'I118uailfriedx151201005423521'

and another like so:

>>> RE_SID = re.compile(r'(?P<sid>(?<=sid:<<")([A-Za-z0-9]+))')
>>> x = RE_SID.search('sid:<<"I118uailfriedx151201005423521')
>>> x.group('sid')
'I118uailfriedx151201005423521'

How can I combine these two patterns in a way that, after parsing these two different lines,:

sid:A111uancalual2626x151130185758596
sid:<<"I118uailfriedx151201005423521">>

returns only the corresponding id to me.

like image 797
Maryam Pashmi Avatar asked Dec 01 '25 00:12

Maryam Pashmi


1 Answers

RE_SID = re.compile(r'sid:(<<")?(?P<sid>([A-Za-z0-9]+))')

Use this, I've just tested and it is working for me. I've moved some part out.

like image 70
zolo Avatar answered Dec 02 '25 14:12

zolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!