I’m attempting to do the Python challenge. I’m currently stuck on problem #3. The problem gives you a bunch of text, and tells your to extract any lowercase letter surrounded by exactly 3 uppercase letters. For example: XXXsXXX should return s as a match.
I’m came up with this regex to perform to look for matches.
message = re.findall('(?<=[A-Z]{3})[a-z](?=...[A-Z]{3})', data)
What I think this does is:
- Look for 3 uppercase letters before a lowercase letter.
- Look for 3 uppercase letters after a lowercase letter.
- Return said lowercase letter.
Is this right?
The problem with your regex is the
...inside of the lookahead, you are basically looking for any three characters followed by three uppercase characters after the lowercase. For example:You can fix this issue by just getting rid of the three
...in the lookahead, however if you need to match exactly three uppercase or lowercase you will need to modify your regex to something like the following:You may need to deal with the special cases of the string starting or ending with a valid pattern.