非捕获组

官网的

  1. (?...)
  2. This is an extension notation (a '?' following a '(' is not meaningful otherwise).
  3. The first character after the '?' determines what the meaning and further syntax of the construct is.
  4. Extensions usually do not create a new group; (?P<name>...) is the only exception to this rule.
  5. Following are the currently supported extensions.
  6. (?:...)
  7. A non-capturing version of regular parentheses.
  8. Matches whatever regular expression is inside the parentheses,
  9. but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.
  10. (?P<name>...)
  11. Similar to regular parentheses, but the substring matched by the group is accessible via the symbolic group name name.
  12. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression.
  13. A symbolic group is also a numbered group, just as if the group were not named.
  1. (?…)
  2. 这是个扩展标记法 (一个 '?' 跟随 '(' 并无含义)。
  3. '?' 后面的第一个字符决定了这个构建采用什么样的语法。
  4. 这种扩展通常并不创建新的组合; (?P<name>...) 是唯一的例外。 以下是目前支持的扩展。
  5. (?:…)
  6. 正则括号的非捕获版本。 匹配在括号内的任何正则表达式,
  7. 但该分组所匹配的子字符串 不能 在执行匹配后被获取或是之后在模式中被引用。
  8. (?P<name>…)
  9. (命名组合)类似正则组合,但是匹配到的子串组在外部是通过定义的 name 来获取的。
  10. 组合名必须是有效的Python标识符,并且每个组合名只能用一个正则表达式定义,只能定义一次。
  11. 一个符号组合同样是一个数字组合,就像这个组合没有被命名一样。
  12. 命名组合可以在三种上下文中引用。
  13. 如果样式是 (?P<quote>['"]).*?(?P=quote)
  14. (也就是说,匹配单引号或者双引号括起来的字符串):
  15. 引用组合 "quote" 的上下文 引用方法
  16. 在正则式自身内 (?P=quote) (如示)
  17. \1
  18. 处理匹配对象 m m.group('quote')
  19. m.end('quote') (等)
  20. 传递到 re.sub() 里的 repl 参数中 \g<quote>
  21. \g<1>
  22. \1

代码演示如何使用:

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # Author: Crifan Li
  4. # Update: 20191223
  5. # Function: Demo python re (?:…) non-capture usage
  6. import re
  7. def demoReNonCapturingGroup():
  8. normalGroupPattern = """(请?点击[“”"'])(.+?)([“”"'])"""
  9. nonCapturingGroupPattern = """(?:请?点击[“”"'])(?:.+?)(?:[“”"'])"""
  10. namedGroupPattern = """(请?点击[“”"'])(?P<strToClick>.+?)([“”"'])"""
  11. inputStrList = [
  12. "请点击“升级”按钮取210000经验,需50元宝提取经验",
  13. "点击”+“,放入吞噬道具",
  14. "请点击”战骑“",
  15. ]
  16. for eachInputStr in inputStrList:
  17. print("="*60)
  18. foundNormalGroup = re.search(normalGroupPattern, eachInputStr)
  19. foundNonCapturingGroup = re.search(nonCapturingGroupPattern, eachInputStr)
  20. foundNamedGroup = re.search(namedGroupPattern, eachInputStr)
  21. if foundNormalGroup and foundNonCapturingGroup and foundNamedGroup:
  22. print("-"*60)
  23. print("eachInputStr=%s -> " % eachInputStr)
  24. matchedWholeStrNormal = foundNormalGroup.group(0)
  25. print("matchedWholeStrNormal=%s" % matchedWholeStrNormal)
  26. matchedWholeStrNonCapturing = foundNonCapturingGroup.group(0)
  27. print("matchedWholeStrNonCapturing=%s" % matchedWholeStrNonCapturing)
  28. matchedWholeStrNamed = foundNamedGroup.group(0)
  29. print("matchedWholeStrNamed=%s" % matchedWholeStrNamed)
  30. matchedGroupListNormal = foundNormalGroup.groups()
  31. print("matchedGroupListNormal=%s" % (matchedGroupListNormal, ))
  32. matchedGroupListNonCapturing = foundNonCapturingGroup.groups()
  33. print("matchedGroupListNonCapturing=%s" % (matchedGroupListNonCapturing, ))
  34. matchedGroupListNamed = foundNamedGroup.groups()
  35. print("matchedGroupListNamed=%s" % (matchedGroupListNamed, ))
  36. strToClickNormal = foundNormalGroup.group(2)
  37. print("strToClickNormal=%s" % strToClickNormal)
  38. strToClickNamed = foundNamedGroup.group("strToClick")
  39. print("strToClickNamed=%s" % strToClickNamed)
  40. pass
  41. pass
  42. # ============================================================
  43. # ------------------------------------------------------------
  44. # eachInputStr=请点击“升级”按钮取210000经验,需50元宝提取经验 ->
  45. # matchedWholeStrNormal=请点击“升级”
  46. # matchedWholeStrNonCapturing=请点击“升级”
  47. # matchedWholeStrNamed=请点击“升级”
  48. # matchedGroupListNormal=('请点击“', '升级', '”')
  49. # matchedGroupListNonCapturing=()
  50. # matchedGroupListNamed=('请点击“', '升级', '”')
  51. # strToClickNormal=升级
  52. # strToClickNamed=升级
  53. # ============================================================
  54. # ------------------------------------------------------------
  55. # eachInputStr=点击”+“,放入吞噬道具 ->
  56. # matchedWholeStrNormal=点击”+“
  57. # matchedWholeStrNonCapturing=点击”+“
  58. # matchedWholeStrNamed=点击”+“
  59. # matchedGroupListNormal=('点击”', '+', '“')
  60. # matchedGroupListNonCapturing=()
  61. # matchedGroupListNamed=('点击”', '+', '“')
  62. # strToClickNormal=+
  63. # strToClickNamed=+
  64. # ============================================================
  65. # ------------------------------------------------------------
  66. # eachInputStr=请点击”战骑“ ->
  67. # matchedWholeStrNormal=请点击”战骑“
  68. # matchedWholeStrNonCapturing=请点击”战骑“
  69. # matchedWholeStrNamed=请点击”战骑“
  70. # matchedGroupListNormal=('请点击”', '战骑', '“')
  71. # matchedGroupListNonCapturing=()
  72. # matchedGroupListNamed=('请点击”', '战骑', '“')
  73. # strToClickNormal=战骑
  74. # strToClickNamed=战骑
  75. # 结论:
  76. # non-capturing group = 非捕获组
  77. # 含义:正常去匹配,但是匹配后的match的group中,是无法获取到对应的值的
  78. # 用途:(感觉唯一的用途就只是)在匹配时,用group去匹配,容易看懂相关内容的逻辑和关系,但是匹配的结果中,不关心这部分的内容
  79. if __name__ == "__main__":
  80. demoReNonCapturingGroup()