【问题标题】:Atom regexp: discarding multiline text around blocksAtom regex:丢弃块周围的多行文本
【发布时间】:2021-10-23 05:17:34
【问题描述】:

假设我有这个文本:

blah blah Bob Loblaw Law blah
keep1 { i want this } blop
blah blob keep2 { and
this too } blaw blat
etc...

我想结束

keep1 { i want this }
keep2 { and
this too }

或许:

keep1 { i want this }
keep2 { and this too }

我还没有弄清楚如何让 Atom 的正则表达式查找/替换机制在特定匹配字符串的多行之外丢弃所有内容。提示?

更新:

在我尝试过的许多事情中,这让我最接近:

[\S\s]+?(keep\d\s+\{[\S\s]+?\})

导致:

keep1 { i want this }
keep2 { and
this too }
 blaw blat
etc...

这可能已经足够了——我可以编辑尾随分片——但知道如何修剪它们也会很有用。

【问题讨论】:

  • @anubhava 当然——我在转向 S.O. 之前尝试了很多事情。我已经用我得到的最好结果更新了这个问题。
  • 这个怎么样:keep\d\s+\{[^}]*\} - 这在 atom-editor 中有效吗?

标签: regex atom-editor


【解决方案1】:

使用

[\s\S]*?(keep\d\s+\{[^{}]*\})|(?:(?!keep\d\s+\{[^{}]*\})[\s\S])+$

proof

解释

--------------------------------------------------------------------------------
  [\s\S]*?                 any character of: whitespace (\n, \r, \t,
                           \f, and " "), non-whitespace (all but \n,
                           \r, \t, \f, and " ") (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    keep                     'keep'
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
    \s+                      whitespace (\n, \r, \t, \f, and " ") (1
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \{                       '{'
--------------------------------------------------------------------------------
    [^{}]*                   any character except: '{', '}' (0 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \}                       '}'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (1 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
      keep                     'keep'
--------------------------------------------------------------------------------
      \d                       digits (0-9)
--------------------------------------------------------------------------------
      \s+                      whitespace (\n, \r, \t, \f, and " ")
                               (1 or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      \{                       '{'
--------------------------------------------------------------------------------
      [^{}]*                   any character except: '{', '}' (0 or
                               more times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
      \}                       '}'
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
    [\s\S]                   any character of: whitespace (\n, \r,
                             \t, \f, and " "), non-whitespace (all
                             but \n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
  )+                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

【讨论】:

    【解决方案2】:

    您可以在 Atom 中使用这个简单的正则表达式替换来完成此任务:

    \b(keep\d+\s*{[^}]*})|.+?
    

    替换为:$1

    RegEx Demo

    正则表达式详细信息:

    • \b:字边界
    • (keep\d+\s*{[^}]*}):在捕获组 #1 中,匹配以 keep 开头的字符串,后跟 1+ 位数字,后跟 0+ 空格,然后是 {...} 内的任何文本,也跨越行。这假设{} 是平衡的,并且{} 没有转义。
    • |:或者
    • .+?: 懒惰匹配 1+ 的任何东西

    PS:如果要删除前导换行符,请使用:

    \n?\b(keep\d+\s*{[^}]*})|.+?
    

    Atom 编辑器演示

    更换前:

    更换后:

    【讨论】:

    • 不依赖于前瞻的好解决方案。
    猜你喜欢
    • 1970-01-01
    • 2013-05-25
    • 2022-10-14
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多