我目前正在为ArcMap 10(updateMessages)中的工具参数编写验证代码,并需要防止用户在字符串中使用非字母数字字符,因为它将用于命名要素类中新创建的字段。
我目前使用了“str.isalnum()”,但这当然排除了下划线。有没有一种有效的方法只接受字母数字字符和下划线?
if self.params[3].altered:
#Check if field name already exists
if str(self.params[3].value) in [f.name for f in arcpy.ListFields(str(self.params[0].value))]:
self.params[3].setErrorMessage("A field with this name already exists in the data set.")
#Check for invalid characters
elif not str(self.params[3].value).isalnum():
self.params[3].setErrorMessage("There are invalid characters in the field name.")
else:
self.params[3].clearMessage()
return
re.match
从字符串开头开始匹配,因此^
是多余的。 - jamylakre.search
。 - jamylak