正则表达式结合前瞻以包含一个字符作为匹配项,但仅当后面没有特定的其他字符时

问题描述 投票:0回答:1
regex pcre
1个回答
0
投票

您不能将lookaground放入字符集中

[]
,请尝试使用OR条件将它们分成两种不同的情况:

<ref name="?((?:[^">\/]|\/(?!>))*?)"? *\/?>
<ref\sname="?    # start of the ref tag, up to the name attribute

(                # group 1, what you want to capture
  (?:            # non-capturing group, for grouping the following OR condition
      [^">\/]    # any character but `">/`
      |          # OR
      \/(?!>)    # if it IS a `/`, then it must NOT be followed by a `>`
  )*?            # this non-capturing group repeats 0 to unlimited times, as few as possible (to avoid capturing trailing whitespaces)
)                # group 1 ends

"?\s*\/?>        # everything after the name attribute

在此处检查测试用例。

但请注意,使用正则表达式匹配 HTML 标签通常被认为是

是个坏主意。它只适用于一些非常基本的验证,对于复杂的验证,建议使用一些 DOM 解析库来处理它。

© www.soinside.com 2019 - 2024. All rights reserved.