I can't match the question mark character although I escaped it.
I tried escaping with multiple backslashes and also using re.escape()
.
What am I missing?
Code:
import re
text = 'test?'
result = ''
result = re.match(r'\?',text)
print ("input: "+text)
print ("found: "+str(result))
Output:
input: test?
found: None
re.match
only matches a pattern at the begining of string; as in the docs:
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object.
so, either:
>>> re.match(r'.*\?', text).group(0)
'test?
or re.search
>>> re.search(r'\?', text).group(0)
'?'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句