简体   繁体   English

Python正则表达式将所有字符与字符集匹配

[英]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>

Anchor the regex to restrict it.锚定正则表达式以限制它。 r'^[az]*$'

re.search(r'^[az]*$','abcA') This will do the job. re.search(r'^[az]*$','abcA')这将完成这项工作。 ^ implies the start of a string, while $ implies the end of a string. ^表示字符串的开始,而$表示字符串的结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM