import re
if re.match(r'^\w+$', text):
Only accept alphanumeric characters and underscores for a string in python
-
31-05-2022 - |
Domanda
I'm currently writing validation code for a tool parameter in ArcMap 10 (updateMessages) and need to prevent users from using non-alphanumeric characters within a string as it will be used to name a newly created field in a feature class.
I have so far used 'str.isalnum()' however this of course excludes underscores. Is there an efficient way to only accept alphanumeric characters and underscores?
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
Soluzione 2
Altri suggerimenti
Try regular expressions:
import re
if re.match(r'^[A-Za-z0-9_]+$', text):
# do stuff
An alternative way, without using regular expression for this specific case :
if text.replace('_', '').isalnum():
# do stuff
You can also check for ascii characters only :
if text.replace('_', '').isalnum() and text.isascii():
# do stuff
If you are using Python3 and there are non-ASCII characters in your string it is better to compile the regex with 8-bit string setting.
import sys
import re
if sys.version_info >= (3, 0):
_w = re.compile("^\w+$", re.A)
else:
_w = re.compile("^\w+$")
if re.match(_w, text):
pass
For more information please refer to here.