Nitish Nitish - 7 months ago 4
Python Question

Regular expression to match 3 capital letters followed by a small letter followed by 3 capital letters?

I need a regular expression in python which matches exactly 3 capital letters followed by a small letter followed by exactly 3 capital letters.
For example, it should match ASDfGHJ and not ASDFgHJK.

agf agf
Answer
r'\b[A-Z]{3}[a-z][A-Z]{3}\b'

This will match what you posted if it is a complete word.

r'(?<![^A-Z])[A-Z]{3}[a-z][A-Z]{3}(?![A-Z])'

This will match what you posted so long as it's not preceded or followed by another capital letter.

Comments