【问题标题】:Escape Quotes -How to escape the plus sign转义引号 - 如何转义加号
【发布时间】:2021-03-21 07:54:08
【问题描述】:
import re

这是我的尝试

note = r"Call \+201099973073\.Midnight"

note = "Call +201099973073.Midnight"

那么

print(re.search("+201099973073",note))

错误

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
~/Desktop/modules/regular_exp.py in 
     12 note = "Call +201099973073.Midnight"
     13 
---> 14 print(re.search("+201099973073",note))

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/re.py in search(pattern, string, flags)
    199     """Scan through string looking for a match to the pattern, returning
    200     a Match object, or None if no match was found."""
--> 201     return _compile(pattern, flags).search(string)
    202 
    203 def sub(pattern, repl, string, count=0, flags=0):

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/re.py in _compile(pattern, flags)
    302     if not sre_compile.isstring(pattern):
    303         raise TypeError("first argument must be string or compiled pattern")
--> 304     p = sre_compile.compile(pattern, flags)
    305     if not (flags & DEBUG):
    306         if len(_cache) >= _MAXCACHE:

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_compile.py in compile(p, flags)
    762     if isstring(p):
    763         pattern = p
--> 764         p = sre_parse.parse(p, flags)
    765     else:
    766         pattern = None

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_parse.py in parse(str, flags, state)
    946 
    947     try:
--> 948         p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
    949     except Verbose:
    950         # the VERBOSE flag was switched on inside the pattern.  to be

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_parse.py in _parse_sub(source, state, verbose, nested)
    441     start = source.tell()
    442     while True:
--> 443         itemsappend(_parse(source, state, verbose, nested + 1,
    444                            not nested and not items))
    445         if not sourcematch("|"):

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_parse.py in _parse(source, state, verbose, nested, first)
    666                 item = None
    667             if not item or item[0][0] is AT:
--> 668                 raise source.error("nothing to repeat",
    669                                    source.tell() - here + len(this))
    670             if item[0][0] in _REPEATCODES:

error: nothing to repeat at position 0

【问题讨论】:

  • 为什么 -print('+201099973073' in note)- 工作而不转义?
  • 因为+ 是正则表达式中的元字符,而不是字符串。在正则表达式中+ 表示“之前的一个或多个事物”,但在字符串中它仅表示+。因此,如果您希望您的正则表达式识别+,那么您必须输入"\+" 来表示您的意思是一个加号,而不是“前面的一个或多个东西”。这就是问题开始的地方,因为 \ 是 Python 字符串中的元字符,您将其提供给re 的编译器(但不在正则表达式中)。所以你必须使用原始字符串r"\+" 或转义反斜杠:"\\+"
  • 非常感谢!

标签: python string escaping jupyter python-re


【解决方案1】:
note = r"Call \+201099973073\.Midnight"
print(re.search("+201099973073",note))

需要转义的不是搜索字符串,而是正则表达式。如果您将尝试的转义应用于search 的第一个参数而不是note,它将起作用:

note = "Call +201099973073.Midnight"
print(re.search(r"\+201099973073", note))

输出:

<re.Match object; span=(5, 18), match='+201099973073'>

【讨论】:

  • 为什么 -print('+201099973073' in note)- 工作而不转义?
  • in 不使用正则表达式。它执行一个简单的子字符串搜索,没什么特别的。更简单,更高效,转义不适用。
猜你喜欢
  • 1970-01-01
  • 2017-01-08
  • 2011-08-28
  • 1970-01-01
  • 2013-08-09
  • 1970-01-01
  • 2011-04-19
相关资源
最近更新 更多