1

我只需要有人纠正我对这个 regex 的理解,这就像匹配 HTML 标签的权宜之计。

< (?: "[^"]*" ['"]* | '[^']*'['"]*|[^'">])+ >

我的理解 -

  • < - 匹配标签打开符号
  • (?:- 无法理解这里发生了什么。这些符号是什么意思?
  • "[^"]*['"]*双引号中的任意字符串。还有别的东西吗?
  • '[^']*'['"]*- 单引号中的一些字符串
  • [^'">]- '" > 以外的任何字符。

所以它是一个 '<' 符号。后跟双引号或单引号中的字符串或任何其他包含 '" 或 > 的字符串,重复一次或多次后跟一个 '>' 。
这是我能做出的最好的.

4

3 回答 3

5
<       # literally just an opening tag followed by a space
(       # the bracket opens a subpattern, it's necessary as a boundary for
        # the | later on
?:      # makes the just opened subpattern non-capturing (so you can't access it
        # as a separate match later
"       # literally "
[^"]    # any character but " (this is called a character class)
*       # arbitrarily many of those (as much as possible)
"       # literally "
['"]    # either ' or "
*       # arbitrarily many of those (and possible alternating! it doesn't have
        # to be the same character for the whole string)
|       # OR
'       # literral *
[^']    # any character but ' (this is called a character class)
*       # arbitrarily many of those (as much as possible)
'       # literally "
['"]*   # as above
|       # OR
[^'">]  # any character but ', ", >
)       # closes the subpattern
+       # arbitrarily many repetitions but at least once
>       # closing tag

请注意,正则表达式中的所有空格都被视为与任何其他字符一样。它们恰好匹配一个空格。

还要特别注意^字符类开头的 。它不被视为单独的字符,而是反转整个字符类。

我也可以(强制性地)补充说,正则表达式不适合解析 HTML。

于 2012-10-04T07:38:53.613 回答
2

用 s 分割它|,表示ors:

<
  (?:
    "[^"]*" ['"]* |
    '[^']*'['"]* |
    [^'">]
  )+
>

(?:表示不匹配的组。该组的内部匹配这些东西(按此顺序):

  1. "stuff"
  2. 'stuff'
  3. asd=

实际上,这是一个尝试将 HTML 标记与属性匹配的正则表达式。

于 2012-10-04T07:40:42.567 回答
0

这是 YAPE::Regex::Explain 的结果

(?-imsx:< (?: "[^"]*" ['"]* | '[^']*'['"]*|[^'">])+ >)

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, but do not capture (1 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
     "                       ' "'
----------------------------------------------------------------------
    [^"]*                    any character except: '"' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    "                        '" '
----------------------------------------------------------------------
    ['"]*                    any character of: ''', '"' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
                             ' '
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
     '                       ' \''
----------------------------------------------------------------------
    [^']*                    any character except: ''' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    '                        '\''
----------------------------------------------------------------------
    ['"]*                    any character of: ''', '"' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    [^'">]                   any character except: ''', '"', '>'
----------------------------------------------------------------------
  )+                       end of grouping
----------------------------------------------------------------------
   >                       ' >'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2012-10-04T08:07:32.487 回答