【问题标题】:Python RegEx MeaningPython正则表达式含义
【发布时间】:2011-10-31 18:14:01
【问题描述】:

我是 python 正则表达式的新手,想知道是否有人可以通过引导我了解这意味着什么来帮助我(我也会在这里说明我认为每个位的含义)。

谢谢!

RegExp:
r'(^.*def\W*)(\w+)\W*\((.*)\):'

r'...' = python definition of regular expression within the ''
(...) = a regex term
(^. = match the beginning of any character
*def\W* = ???
(\w+) = match any of [a, z] 1 or more times
\W*\ = ? i think its the same as the line above this but from 0+ more times instead of 1 but since it matches the def\W line above (which i dont really know the meaning of) i'm not sure.
((.*)\): = match any additional character within brackets ()

谢谢!

【问题讨论】:

  • +1 用于展示您尝试过的内容
  • 另外,这些是否会导致出现空白?

标签: python regex


【解决方案1】:

似乎尝试匹配 Python function signature 失败:

import re

regex = re.compile(r""" # r'' means that \n and the like is two chars
                        # '\\','n' and not a single newline character

    ( # begin capturing group #1; you can get it: regex.match(text).group(1)
      ^   # match begining of the string or a new line if re.MULTILINE is set
      .*  # match zero or more characters except newline (unless
          # re.DOTALL is set)
      def # match string 'def'
      \W* # match zero or more non-\w chars i.e., [^a-zA-Z0-9_] if no
          # re.LOCALE or re.UNICODE
    ) # end capturing group #1

    (\w+) # second capturing group [a-zA-Z0-9_] one or more times if
          # no above flags

    \W*   # see above

    \(    # match literal paren '('
      (.*)  # 3rd capturing group NOTE: `*` is greedy `.` matches even ')'
            # therefore re.match(r'\((.*)\)', '(a)(b)').group(1) == 'a)(b'
    \)    # match literal paren ')'
     :    # match literal ':'
    """, re.VERBOSE|re.DEBUG)

re.DEBUG 标志导致输出:

subpattern 1
  at at_beginning
  max_repeat 0 65535
    any None
  literal 100
  literal 101
  literal 102
  max_repeat 0 65535
    in
      category category_not_word
subpattern 2
  max_repeat 1 65535
    in
      category category_word
max_repeat 0 65535
  in
    category category_not_word
literal 40
subpattern 3
  max_repeat 0 65535
    any None
literal 41
literal 58

more

【讨论】:

    【解决方案2】:

    r'..' = Python 用于正则表达式模式的原始字符串表示法;在以 'r' 为前缀的字符串文字中,不会以任何特殊方式处理反斜杠。

    (...) = 一个捕获组,它将捕获的值存储在 var 中以用于替换/数学运算。

    ^ = 字符串的开头。

    .* = 0 个或多个任意类型的字符。

    def = 文字字符串 def

    \W* = 0 个或多个非单词字符(a-zA-Z 或 _ 以外的任何字符)

    \w+ = 1 个或多个单词字符(见上文)

    \( = 转义 (,因此表示文字 (

    \) = 同上。

    : = 文字:


    PS:我喜欢你为理解正则表达式所做的努力。它会很好地为您服务,比人们问r'(^.*def\W*)(\w+)\W*\((.*)\):' 是什么意思要好得多。

    【讨论】:

    • 很高兴您对 OP 自己解决问题的努力给予了赞扬。
    • . 不匹配换行符,除非 re.DOTALL^ 取决于 re.MULTILINE\W\w 依赖于 re.UNICODEre.LOCALE。见my answer
    【解决方案3】:

    r'...' = python 定义内的正则表达式''

    r'' 语法与正则表达式无关(或至少,不是直接的)。 r 代表 raw,它只是 Python 的一个指示符,表示不应对字符串执行字符串插值。

    这通常与正则表达式一起使用,这样您就不必转义反斜杠 (\) 字符,否则这些字符会被正常的字符串插值机制吃掉。

    (^. = 匹配任意字符的开头

    我不确定“任何字符的开头”是什么意思。 ^ 字符匹配行首。

    def\W = ???

    def 匹配字符 def。对于\W,请看pydoc re,它描述了正则表达式语言。

    \W*

    如上。

    除上述之外,您的解释似乎基本正确。

    【讨论】:

      【解决方案4】:

      r'(^.*def\W\*)(\w+)\W*((.*)):'

      ==================================
      
      r' tells python this is a raw string so you don't have to double escape all the \
      
      ^ match start of string
      
      () in each case this means group this match where each () is a different group
      
      .* match zero or more of any characters
      
      def match the literal 'def'
      
      \W* match zero or more of any non word character
      
      () more grouping of the contained expression
      
      \w+ match one or more of word character
      
      \W* zero or more of any non word character
      
      \( escape the left paren
      
      () more grouping of the contained expression
      
      .* zero of more of any character
      
      \) escape the right paren
      
      : match a single colon literal
      

      这看起来像是在尝试匹配 python 方法定义。 Here is a link to play with this regular expression. 是的,它由 Ruby 提供支持,但所有语言的语法几乎相同,我使用这个站点来测试 Python、Java 和 Ruby 的正则表达式。

      【讨论】:

        【解决方案5】:
        1. 原始字符串表示法(r“text”)使正则表达式保持理智。没有它,正则表达式中的每个反斜杠 ('\') 都必须以另一个作为前缀来转义它。

        2. ( ) 对语句进行分组,它被视为一件事情,所以你可以做 () 吗?或者 ()* 或者 ()+ 如果括号里的东西需要一起处理。

        3. ^ 匹配它,如果它是字符串的开头

        4. (点。)在默认模式下,它匹配除换行符以外的任何字符。

        5. 既然是“.*”,*表示匹配前一个事物的0个或多个匹配,在这种情况下是任何字符。

        6. def\W* - 以字符串“def”开头然后 \W 匹配任何非字母数字字符的行,等效于 [^a-zA-Z0-9_]。由于我们又得到了 *,这一次它匹配 0 个或多个非字母数字字符。

        7. (\w+),+代表前面的1个或多个,在本例中为\w,等价于[a-zA-Z0-9_]。

        7.\W*,我们已经知道了。

        1. "(" - 表示只匹配 "(",区别于 () 分组事物,同样适用于 ")/"

        2. (.*) - 匹配 0 个或多个字符。

        3. : - 匹配的字符串以冒号结尾。

        整个事情似乎与 python 中的函数定义相匹配,即“def foo(x):”将被匹配。处理正则表达式很困难——使用诸如http://www.pythonregex.com/ 之类的工具可以帮助我尝试不同的东西。而且由于 RE 在不同语言中略有不同,因此也可以为这些语言提供工具。

        【讨论】:

          【解决方案6】:
          • r'...' → 在 Python 中定义正则表达式字符串的首选方式
          • (...) → 正则表达式术语
          • ^ → 只匹配字符串的开头

          所以,在第一对括号(^.def\W)中,首先匹配的是字符串。

          • 。 → 匹配任何字符
          • * → 重复上一场比赛 0 次或更多次

          然后 .* 将匹配任意次数的任何内容。下面的'def'是完全匹配,只匹配自身。

          • \W → 匹配任何非字母、非数字或下划线字符。

          然后 \W* 将匹配零个或多个这些非字母数字下划线字符。下一对括号 (\w+) 你猜对了。在最后一部分 \W*\((.*)\):最初的 \W* 与前面的 \W* 含义相同。接下来,\( 与 ( 匹配,然后是组 (.*),与之前一样,表示任何次数的任何内容,然后是匹配 ): 的 \):。

          与此正则表达式匹配的字符串示例如下:

          thing_def = function_name (123 anything in here):
          

          【讨论】:

            猜你喜欢
            • 2020-07-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-18
            • 2010-09-12
            • 2011-08-06
            • 1970-01-01
            相关资源
            最近更新 更多