Python 正则表达式的含义

标签 python regex

我是 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 ()

谢谢!

最佳答案

这似乎是一次失败的匹配 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

关于Python 正则表达式的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7957846/

相关文章:

c++ - 元正则表达式 : test if regex is only a string (no regex "wildcards")

php - 正则表达式 匹配字符串

java - 用java正则表达式替换

python - 如何退出 python execfile() 而不退出整个脚本

python - 获取文件的最后访问时间?

python - 新手在 Python 中遇到简单的 "while"重复

java - 日语字符串的模式匹配在 java 中有问题

Python 正则表达式 - 匹配不在 mustache 括号中的所有内容

python - 如何将整数迭代 append 到 python 中的字符串中?

regex - 如何获取R中两个字符第一次出现之间的模式?