The problem with using match() here is that it is anchored to the beginning of the string. You want to find a single special character anywhere in the string, so use search() instead:
def no_special_characters(s, pat=re.compile('[@_!#$%^&*()<>?/\|}{~:]')):
if pat.search(s):
print(s + " has special characters")
else:
print(s + " has NO special characters")
You could also keep using match() with the following regex pattern:
.*[@_!#$%^&*()<>?/\|}{~:].*
Answer from Tim Biegeleisen on Stack OverflowThe problem with using match() here is that it is anchored to the beginning of the string. You want to find a single special character anywhere in the string, so use search() instead:
def no_special_characters(s, pat=re.compile('[@_!#$%^&*()<>?/\|}{~:]')):
if pat.search(s):
print(s + " has special characters")
else:
print(s + " has NO special characters")
You could also keep using match() with the following regex pattern:
.*[@_!#$%^&*()<>?/\|}{~:].*
Use search function instead of match function.
Regex for all special characters
regex - Python: How to match special characters in regular expression? - Stack Overflow
Using regex with special characters to find a match in python - Stack Overflow
Which special characters must be escaped when using Python regex module re? - Stack Overflow
would you say the below is ok to enter in one special character
Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]'
Yes, your expression will match any string that includes at least one of the specified special characters
hi all,
would you say the below is ok to enter in one special character
Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]'
thanks,
rob
Use re.escape
>>> import re
>>> re.escape(r'\ a.*$')
'\\\\\\ a\\.\\*\\$'
>>> print(re.escape(r'\ a.*$'))
\\\ a\.\*\$
>>> re.escape('www.stackoverflow.com')
'www\\.stackoverflow\\.com'
>>> print(re.escape('www.stackoverflow.com'))
www\.stackoverflow\.com
Repeating it here:
re.escape(string)
Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
As of Python 3.7 re.escape() was changed to escape only characters which are meaningful to regex operations.
I'm surprised no one has mentioned using regular expressions via re.sub():
import re
print re.sub(r'([\"])', r'\\\1', 'it\'s "this"') # it's \"this\"
print re.sub(r"([\'])", r'\\\1', 'it\'s "this"') # it\'s "this"
print re.sub(r'([\" \'])', r'\\\1', 'it\'s "this"') # it\'s\ \"this\"
Important things to note:
- In the search pattern, include
\as well as the character(s) you're looking for. You're going to be using\to escape your characters, so you need to escape that as well. - Put parentheses around the search pattern, e.g.
([\"]), so that the substitution pattern can use the found character when it adds\in front of it. (That's what\1does: uses the value of the first parenthesized group.) - The
rin front ofr'([\"])'means it's a raw string. Raw strings use different rules for escaping backslashes. To write([\"])as a plain string, you'd need to double all the backslashes and write'([\\"])'. Raw strings are friendlier when you're writing regular expressions. - In the substitution pattern, you need to escape
\to distinguish it from a backslash that precedes a substitution group, e.g.\1, hencer'\\\1'. To write that as a plain string, you'd need'\\\\\\1'— and nobody wants that.