regex - Python re expression conditional match -


to capture both of following expressions:

str1 = "my son 21 now" str2 = "son 21 now" 

these 2 re statements , filters each capture 1 respectively:

r1 = re.compile(".* (son)\s+(is)\s+(21) .*") r2 = re.compile("(son)\s+(is)\s+(21) .*")  m1 = filter(r1.match, [str1, str2]) m2 = filter(r2.match, [str1, str2]) 

how combine r1 , r2 1 re statement such both strings matched?

i guess easiest make first capturing group optional in first regex:

(.* )?(son)\s+(is)\s+(21) .* 

see demo


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -