【问题标题】:How to capture text between XML Summary?如何在 XML 摘要之间捕获文本?
【发布时间】:2021-04-03 08:20:13
【问题描述】:

我有单行和多行 XML 摘要文本,看起来像这样。

/// <summary> This is a single-line XML comment. </summary> 

/// <summary> This is a multi-line XML comment.
/// These are additional lines with more text.
/// Some more of these text. </summary>

/// <summary> This is another XML text summary with a different
/// format.
/// </summary>

在 RegexBuddy 中,如果没有 /// 和 &lt;summary&gt; &lt;/summary&gt; 标签,我将如何捕获其中的文本?

我想出了以下内容来捕获多行 XML 摘要:

  ((\s*(///)\s*((<summary>)?))(.*))+(</summary>)$

和一个单一的 XML 摘要:

  \s*///\s*(<summary>).*(</summary>)$

但我不知道如何仅捕获文本。

为了只捕获文本,我将使用什么正则表达式,以便在替换参考中使用它?

提前谢谢你。

【问题讨论】:

  • 我不认为它可以用一个正则表达式来完成。我认为您需要提取&lt;summary&gt;&lt;/summary&gt; 之间的所有文本,然后删除/// 字符串。并且不要忘记用于多行摘要的dotall 标志。如果合适且相关,我可以在 java 中发布解决方案。
  • 要获取剩余的文本吗?尝试用空字符串替换^///\s*(?:&lt;summary&gt;\s*)?|\s*&lt;/summary&gt;,参见this demo

标签: regex regexbuddy


【解决方案1】:

使用 PCRE 引擎:

(?:^///\s*(?:<summary>)?|</summary>)(*SKIP)(*F)|(?:(?!</?summary>|^///(?!/)\s*).)+

proof

说明

--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    ^                        the beginning of the string
--------------------------------------------------------------------------------
    ///                      '///'
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      <summary>                '<summary>'
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    </summary>               '</summary>'
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  (*SKIP)                     'SKIP' verb, skips the match
--------------------------------------------------------------------------------
  (*F)                        'FAIL' verb, triggers fail and backtracking
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (1 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
      <                        '<'
--------------------------------------------------------------------------------
      /?                       '/' (optional (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      summary>                 'summary>'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      ^                        the beginning of the string
--------------------------------------------------------------------------------
      ///                      '///'
--------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
        /                        '/'
--------------------------------------------------------------------------------
      )                        end of look-ahead
--------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ")
                               (0 or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
    .                        any character except \n
--------------------------------------------------------------------------------
  )+                       end of grouping

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2012-04-10
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2018-02-23
    相关资源
    最近更新 更多