How to match sequences of non-alphanumeric characters from string with regular expression in Python

1 Answer

0 votes
import re     

s = 'xyx 234 aa4!@!4XY.aaAyyy984a ABCa-xYxx3yYa x aa.'    
pattern = r'\W+'  # sequence of non-alphanumeric characters

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

Found: ' '
Found: ' '
Found: '!@!'
Found: '.'
Found: ' '
Found: '-'
Found: ' '
Found: ' '
Found: '.'

'''

 



answered Apr 27, 2019 by avibootz
...