sarbjit
sarbjit

Reputation: 3894

Python regex matching all the characters with character set

I want to use match all the characters in a string with a set of characters and if any of the character in string is not matching, it should not match. I am using character set and I want all the charcters in string to match character set. But in case if any additional charater is present, it passes.

How can i fix it?

>>> re.search(r'[a-z]*','abcA')  
<_sre.SRE_Match object at 0x026DBBB8> ===> Should FAIL
>>> re.search(r'[a-z]*','abc')
<_sre.SRE_Match object at 0x026DBBF0>

Upvotes: 3

Views: 1773

Answers (2)

AShelly
AShelly

Reputation: 35580

Anchor the regex to restrict it. r'^[a-z]*$'

Upvotes: 7

Jonathan
Jonathan

Reputation: 8891

re.search(r'^[a-z]*$','abcA') This will do the job. ^ implies the start of a string, while $ implies the end of a string.

Upvotes: 2

Related Questions