undefinedfix
Sign in

Python regular expression

Khasky edited in Fri, 20 May 2022

Fb119, how to match FB and 119 separately? Write a few feel very cumbersome, do not know if there is a better way to write. thank you.

3 Replies
kcomf
commented on Fri, 20 May 2022

num = re.search (r'(d)+', ''FB119').group()eng = re.search Is there a better way to write (R '([a-za-z]) +','fb119 '). Group()?

user3599285
commented on Fri, 20 May 2022

Returns a tuple:

pnkjx
commented on Sat, 21 May 2022

I don't know what you mean. Do you mean "named capture"? For example:

>>> s = 'FB119'
>>> import re
>>> m = re.match(r'(?P<a>[a-zA-Z]+)(?P<b>[0-9]+)', s)
>>> m.group('a')
'FB'
>>> m.group('b')
'119'
lock This question has been locked and the reply function has been disabled.