Python正则表达式的含义

2024-04-24 22:52:10 发布

您现在位置:Python中文网/ 问答频道 /正文

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

谢谢!在


Tags: ofthedefmorematchlineanyabove
3条回答

匹配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标志导致输出:

^{pr2}$

more

r'...' = python definition of regular expression within the ''

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

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

(^. = match the beginning of any character

我不知道“任何角色的开头”是什么意思。^字符与行首匹配。在

def\W = ???

def与字符def匹配。对于\W,请看一下描述正则表达式语言的pydoc re。在

\W*

同上。在

除了以上所述,你的解释基本上是正确的。在

r'..' = Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'.

(...) = a capture group which stores the captured value in a var to be used in replacement/mathing.

^ = the start of the string.

.* = 0 or more chars of any type.

def = the literal string def

\W* = 0 or more non word chars (Anything other than a-zA-Z or _)

\w+ = 1 or more word chars (see above)

\( = escapes the (, therefore means a literal (

\) = same as above.

: = literal :


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

相关问题 更多 >