regex - Python Challenge Lvl 3 Explaination (warning spoilers!) -


i doing python challenge , while figured out answer puzzle, did in hacky, not-very-good way. upon advancing able see solution is:

string1 = open('text.txt').read() print ''.join(re.findall('[^a-z][a-z]{3}([a-z])[a-z]{3}[^a-z]', string1)) 

i messed while, removing caret here , seeing happens, changing braced group there. however, cant wrap head around why works. explain in easy understand way?

thank you!

i compiled pattern verbose include inline comments:

pat = re.compile('''     [^a-z]    # character except capital letter     [a-z]{3}  # 3 capital letters     (         # beginning of capturing group     [a-z]     # 1 lowercase letter      )         # end of group     [a-z]{3}  # 3 capital letters     [^a-z]    # character except capital letter     ''', re.verbose) 

demo:

>>> re.findall(pat, 'aaaaabbbbbbbbzzzzzxxxxyyyyywwwwvabcn') ['x', 'v'] 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -