Regex can't escape question mark?

radry

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
behzad.nouri

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I escape a literal question mark ('?') in a JDBC prepared statement

来自分类Dev

Nginx rewrite with question mark

来自分类Dev

Regex - Escape escape characters

来自分类Dev

mark.js的非贪婪RegEx

来自分类Dev

Bogus escape error when running regex

来自分类Dev

RegEx:使用 Escape \= 提取 Key=Value 对

来自分类Dev

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 292-293: truncated \UXXXXXXXXX escape during import

来自分类Dev

C ++ std :: regex垃圾管道error_escape异常

来自分类Dev

Can Regex switch charactors in a string?

来自分类Dev

使用Regex.Escape时,正则表达式不匹配

来自分类Dev

Can not catch substring by regex which ends with tab

来自分类Dev

Can't disable UIBarButtonItem

来自分类Dev

Can't Add to an Array

来自分类Dev

Can't compile SocialBootstrapApi

来自分类Dev

Why isn't this simple REGEX working?

来自分类Dev

T-SQL中的REGEX替换

来自分类Dev

在vim中不能使用'mark a'或'mark A'

来自分类Dev

在vim中不能使用'mark a'或'mark A'

来自分类Dev

Can't link two tables?

来自分类Dev

Can't send email to myself

来自分类Dev

ItextSharp can't find PDF

来自分类Dev

GitHub for Windows can't log in

来自分类Dev

Can't observe computed property

来自分类Dev

Can't encode JSON data

来自分类Dev

QCommandLineOption can't get option

来自分类Dev

xterm: Can't execvp ulimit

来自分类Dev

Uribuilder encoding with exclamation mark

来自分类Dev

Shortcut to mark directory as source

来自分类Dev

启用FileReader mark()/ reset()