我正在尝试使用Python regular expression来验证变量的值。
验证规则如下:
- 该值可以包含任何
a-z,A-Z,0-9和*(noblank, no-, no,) - 该值可以
start带有数字(0-9)或字母(a-z, A-Z)或* - 该值可以
end带有数字(0-9)或字母(a-z, A-Z)或* - 在中间,值可以包含数字
(0-9)或字母(a-z, A-Z)或* - 不得允许任何其他值
目前我正在使用以下代码片段进行验证:
import re
data = "asdsaq2323-asds"
if re.compile("[a-zA-Z0-9*]+").match(data).group() == data:
print "match"
else:
print "no match"
我觉得应该有更好的方法来完成上述工作。我正在寻找类似以下的内容:
validate_func(pattern, data)
/* returns data if the data passes the validation rules */
/* return None if the data does not passes the validation rules */
/* should not return part of the data which matches the validation rules */
是否存在这样一种内置功能?