删除除特定模式之外的所有内容 [英] Remove everything except a certain pattern

查看:56
本文介绍了删除除特定模式之外的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多字符串的日志文件.我想从这个文件中删除所有内容(查找和替换),除了以 phone= 开头并以 Digits=1

I have a log file with lots of strings. I would like to remove everything from this file (find & replace) except any string that starts with: phone= and ended with Digits=1

例如:phone=97212345678&step=1&digits=1

要找到我使用的字符串 (phone=.*digits=1) 并且它有效!但我没有设法找到正则表达式,选择除此字符串之外的所有内容并将其全部清除.

To find that string I am using (phone=.*digits=1) and it works! but I did not manage to find the regex the select everything but this string and to clear them all.

示例文件.

推荐答案

为了删除特定文本以外的任何内容,您需要使用 .*(text_you_need_to_keep).*. 匹配换行符.

In order to remove anything but a specific text, you need to use .*(text_you_need_to_keep).* with . matching a newline.

在记事本++中,使用

      查找:.*(phone=\S*?digits=1).*
替换:$1

       Find: .*(phone=\S*?digits=1).*
Replace: $1

注意:.必须选中匹配换行选项.

NOTE: . matches newline option must be checked.

我在捕获模式中使用 \S*? 而不是 .* 因为您只想匹配 中尽可能少的任何非空白字符phone= 到最接近的 digits..* 过于贪婪,可能会在 DOTALL 选项打开的情况下跨越多行.

I use \S*? instead of .* inside the capturing pattern since you only want to match any non-whitespace characters as few as possible from phone= up to the closest digits. .* is too greedy and may stretch across multiple lines with DOTALL option ON.

更新

当您想在文本中保留一些多次出现的模式时,在 Notepad++ 中,您可以使用

When you want to keep some multiple occurrences of a pattern in a text, in Notepad++, you can use

.*?(phone=\S*?digits=1)

替换为 $1\n.这样,您将删除所有不需要的子字符串,但在最后一次出现必要子模式之后的子字符串.

Replace with $1\n. With that, you will remove all the unwanted substrings but those after the last occurrence of your necessary subpattern.

您需要手动或使用删除最后一个块

You will need to remove the last chunk either manaully or with

   FIND: (phone=\S*?digits=1).*
REPLACE: $1

这篇关于删除除特定模式之外的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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