How to find pattern matches with ? in a string with regular expression in Python

1 Answer

0 votes
import re     

s = 'xyxxxyyyxyxxyy'    
pattern = 'xy?'  # 'x' followed by zero or one 'y' 

for match in re.findall(pattern, s):         
    print('Found: {!r}'.format(match))
    
    
'''
run:

Found: 'xy'
Found: 'x'
Found: 'x'
Found: 'xy'
Found: 'xy'
Found: 'x'
Found: 'xy'

'''

 



answered Apr 27, 2019 by avibootz
...