I'm trying to check if a string in python contains escaped characters. The simplest way to do this is to set a list of escaped characters, then check if any element in the list is in the string:
s = "A & B"
escaped_chars = ["&",
""",
"'",
">"]
for char in escaped_chars:
if char in s:
print "escape char '{0}' found in string '{1}'".format(char, s)
Is there a better way of doing this?
You can use regular expression (See also re
module documentation):
>>> s = "A & B"
>>> import re
>>> matched = re.search(r'&\w+;', s)
>>> if matched:
... print "escape char '{0}' found in string '{1}'".format(matched.group(), s)
...
escape char '&' found in string 'A & B'
&
, ;
matches &
, ;
literally.\w
matches word character (alphabet, digits, _
).\w+
matches one or more word characters.