python - Regex statement to check for 3 capital letters? -


i need regex statement check 3 capital letters in row.

for example should match: abc, aabc, abcabc

but should not match: aabbbc, abcde

at moment statement:

'[^a-z]*[a-z]{3}[^a-z]*' 

but matches abcde, doing wrong?

thanks in advance.

regex

(?<![a-z])[a-z]{3}(?![a-z]) 

explanation

i specified negative lookbehind , negative lookahead before , after middle regex 3 capitals in row, respectively.

this better option compared using negated character class because match when there no characters left or right of string.

online demonstration

demo


as python code, haven't figured out how print out actual matches,
syntax:

using re.match:

>>> import re >>> p = re.compile(r'(?<![a-z])[a-z]{3}(?![a-z])') >>> s = '''abc ... aabc ... abcabcabcabcdabcabcdedededa ... abcde''' >>> result = p.match(s) >>> result.group() 'abc' 

using re.search:

>>> import re >>> p = re.compile(r'(?<![a-z])[a-z]{3}(?![a-z])') >>> s = 'abcabcde' >>> p.search(s).group() 'abc' 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -