Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
BlueHost Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

Website Domains Names & Hosting | Domain.com

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Instant Grammar Checker - Correct all grammar errors and enhance your writing

Liquid Web Cloud VPS Hosting

Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

12,685 questions

17,275 answers

573 users

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

6 Answers

0 votes
s = 'xyxxxyyyxyxxyyaaa'    
pattern = '[xy]'  # either 'x' or 'y' 

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

Found: 'x'
Found: 'y'
Found: 'x'
Found: 'x'
Found: 'x'
Found: 'y'
Found: 'y'
Found: 'y'
Found: 'x'
Found: 'y'
Found: 'x'
Found: 'x'
Found: 'y'
Found: 'y'

'''

 




answered Apr 27, 2019 by avibootz
0 votes
import re     

s = 'xyxxxyyyxyxxyyaaa'    
pattern = 'x[xy]'  # 'x' followed by one 'x' or 'y'

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

Found: 'xy'
Found: 'xx'
Found: 'xy'
Found: 'xy'
Found: 'xx'

'''

 




answered Apr 27, 2019 by avibootz
0 votes
import re     

s = 'xyxaaxxaaayyyaaaxyxxyyaaa'    
pattern = 'x[xy]+'  # 'x' followed by one or more 'x' or 'y'

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

Found: 'xyx'
Found: 'xx'
Found: 'xyxxyy'

'''

 




answered Apr 27, 2019 by avibootz
0 votes
import re     

s = 'xyx - aaxx aaayyy -aaaxyxxyyaaa'    
pattern = '[^-]+'  # sequences without -

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

Found: 'xyx '
Found: ' aaxx aaayyy '
Found: 'aaaxyxxyyaaa'

'''

 




answered Apr 27, 2019 by avibootz
0 votes
import re     

s = 'xyx - aaxx .aaayyy -aaaxyxxyyaaa.'    
pattern = '[^-.]+'  # sequences without - and .

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

Found: 'xyx '
Found: ' aaxx '
Found: 'aaayyy '
Found: 'aaaxyxxyyaaa'

'''

 




answered Apr 27, 2019 by avibootz
0 votes
import re     

s = 'xyx - aaxx .aaayyy -aaaxyxxyyaaa.'    
pattern = '[^ ]+'  # sequences without space

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

Found: 'xyx'
Found: '-'
Found: 'aaxx'
Found: '.aaayyy'
Found: '-aaaxyxxyyaaa.'

'''

 




answered Apr 27, 2019 by avibootz
...