在 Perl 中的两个符号之间找不到模式时,如何删除模式? [英] How to delete a pattern when it is not found between two symbols in Perl?

查看:32
本文介绍了在 Perl 中的两个符号之间找不到模式时,如何删除模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的文档:

Once upon a time, there lived a cat.
The AAAAAA cat was ZZZZZZ very happy.
The AAAAAAcatZZZZZZ knew many other cats from many AAAAAA cities ZZZZZZ.
The cat knew brown cats and AAAAAA green catsZZZZZZ and red cats.

AAAAAAZZZZZZ 类似于 {},但用于避免与其他可能会将 {} 解释为其他含义的脚本.

The AAAAAA and ZZZZZZ are similar to { and }, but are used to avoid problems with other scripts that might interpret { and } as other meanings.

如果在 AAAAAAZZZZZZ 之间找不到cat",我需要删除它的所有外观.

I need to delete all appearances of "cat" when it is not found between an AAAAAA and ZZZZZZ.

Once upon a time, there lived a .
The AAAAAA cat was ZZZZZZ very happy.
The AAAAAAcatZZZZZZ knew many other s from many AAAAAA cities ZZZZZZ.
The  knew brown s and AAAAAA green catsZZZZZZ and red s.

  • 所有 AAAAAA 都有一个匹配的 ZZZZZZ.
  • AAAAAA 和匹配的 ZZZZZZ 不会跨行拆分.
  • AAAAAA 和匹配的 ZZZZZZ 永远不会嵌套.
  • 上例中的模式cat"不被视为单词.这可以是任何东西.
    • All AAAAAA's have a matching ZZZZZZ.
    • The AAAAAA's and matching ZZZZZZ's are not split across lines.
    • The AAAAAA's and matching ZZZZZZ's are never nested.
    • The pattern, "cat" in the example above, is not treated as a word. This could be anything.
    • 我尝试了几种方法,例如:

      I have tried several things, e.g.:

      perl -pe 's/[^AAAAAAA](.*)(cat)(.*)[^BBBBBBB]//g' <<< "AAAAAAA cat 1 BBBBBBB cat 2"
      

      如果在某些匹配的符号集之间找不到任何模式,我该如何删除它?

      How can I delete any pattern when it is not found between some matching set of symbols?

      推荐答案

      你有几种可能的方法:

      1. 您可以使用 \K 功能从匹配结果中删除您不想要的部分:

      1. You can use the \K feature to remove the part you don't want from match result:

      s/AAAAAA.*?ZZZZZZ\K|cat//gs
      

      (\K 从匹配结果中删除左边的所有字符,但左边的所有字符都被正则表达式引擎消耗.结果,当交替的第一部分成功时,你替换带有空字符串的空字符串(紧跟在 ZZZZZZ 之后).

      (\K removes all on the left from match result, but all characters on left are consumed by the regex engine. Consequence, when the first part of the alternation succeeds, you replace the empty string (immediatly after ZZZZZZ) with an empty string.)

      您可以使用 捕获组 进行注入(使用引用 $1) 要保留在替换字符串中的子字符串:

      You can use a capturing group to inject as it (with a reference $1) the substring you want to preserve in the replacement string:

      s/(AAAAAA.*?ZZZZZZ)|cat/$1/gs
      

    • 您可以使用回溯控制动词来跳过和不重试匹配的子串:

    • You can use backtracking control verbs to skip and not retry the substring matched:

      s/AAAAAA.*?ZZZZZZ(*SKIP)(*FAIL)|cat//gs
      

      ((*SKIP) 强制正则表达式引擎在模式稍后失败时不重试左侧找到的子字符串.(*FAIL) 强制模式失败.)

      ((*SKIP) forces the regex engine to not retry the substring found on the left if the pattern fails later. (*FAIL) forces the pattern to fail.)

      注意:如果 AAAAAA 和 ZZZZZZ 必须始终在同一行,您可以删除 /s 修饰符,逐行处理数据.

      Note: if AAAAAA and ZZZZZZ must be always on the same line, you can remove the /s modifier and process the data line by line.

      这篇关于在 Perl 中的两个符号之间找不到模式时,如何删除模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆