簡體   English   中英

在Perl中解碼正則表達式

[英]Decoding regular expression in Perl

任何人都可以解碼此正則表達式在Perl中的含義:

while (/([0-9a-zA-Z\\-]+(?:'[a-zA-Z0-9\\-]+)*)/g)

這是正則表達式的細分:

(                     # start a capturing group (1)
   [0-9a-zA-Z-]+      # one or more digits or letters or hyphens
   (?:                # start a non-capturing group
      '               # a literal single quote character
      [a-zA-Z0-9-]+   # one or more digits or letters or hyphens
   )*                 # repeat non-capturing group zero or more times
)                     # end of capturing group 1

regex的格式為/.../g並處於while循環中,這意味着while內的代碼將針對regex的每個非重疊匹配運行。

有一個專用的工具: YAPE :: Regex :: Explain

The regular expression:

(?-imsx:([0-9a-zA-Z\-]+(?:'[a-zA-Z0-9\-]+)*))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [0-9a-zA-Z\-]+           any character of: '0' to '9', 'a' to
                             'z', 'A' to 'Z', '\-' (1 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      '                        '\''
----------------------------------------------------------------------
      [a-zA-Z0-9\-]+           any character of: 'a' to 'z', 'A' to
                               'Z', '0' to '9', '\-' (1 or more times
                               (matching the most amount possible))
----------------------------------------------------------------------
    )*                       end of grouping
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

FJ的答案是完美的分解。 但是...他遺漏了重要的部分,最后是/ g。 它告訴解析器從上次中斷的地方繼續。 因此,while循環將繼續反復遍歷字符串,直到到達沒有其他點匹配的點為止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM