Regular expression to match 3 capital letters followed by a small letter followed by 3 capital letters? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this questionI 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.
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.
here it is:
'[A-Z]{3}[a-z]{1}[A-Z]{3}'
Edited you need to use word boundary also:
r'\b[A-Z]{3}[a-z]{1}[A-Z]{3}\b'
re.findall(r'[a-z][A-Z]{3}([a-z])[A-Z]{3}[a-z]', data)
This is will check that one small character is exactly present between 3 capital characters on both sides.
I have used lookahead and lookbehind to ensure that exactly three capital characters are on both sides.
r'(?<![A-Z])[A-Z]{3}[a-z][A-Z]{3}(?![A-Z])'.
>>> import re
>>> pattern = r'^[A-Z]{3}[a-z]{1}[A-z]'
>>> re.match(pattern , "ABCaABC").start()
0
>>> print re.match(pattern , "ABCABC")
None
>>> print re.match(pattern , "ABCAABC")
None
>>> print re.match(pattern , "ABCAaABC")
None
>>> print re.match(pattern , "ASDFgHJK")
None
>>> print re.match(pattern , "ABCaABC")
<_sre.SRE_Match object at 0x011ECF70>
r'^[A-Z]{3}[a-z]{1}[A-z]'
^ ->Start with capital, so First three letters must be capital.
精彩评论