How to find pattern matches with {number,number} in a string with regular expression in Python

1 Answer

0 votes
import re     

s = 'xyxxxyyyxyxxyy'    
pattern = 'xy{1,2}'  # 'x' followed by one to two 'y' 

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

Found: 'xy'
Found: 'xyy'
Found: 'xy'
Found: 'xyy'

'''

 



answered Apr 27, 2019 by avibootz
...