简体   繁体   English

如何匹配和删除包含特定字符串的任何行?

[英]How to match and remove any line containing a specific string?

I have a huge directory list of URLs from my Web site. 我有一个庞大的网站目录列表。 Example: 例:

/folder/folder2/folder3/page.htm
/folder/folder2/folder3/page2.htm
/folder/folder2/folder3/page3.htm
/folder/folder2/folder3/page4.htm

I want to clean this list of all items that have /folder2 in the path. 我想清理路径中包含/folder2的所有项目的列表。 I need a regular expression to perform a find and replace for everything that uses /folder2/ and delete those lines from my list. 我需要一个正则表达式来执行查找和替换使用/folder2/所有内容,并从列表中删除这些行。 So find/replace it with blank. 所以用空白查找/替换它。

Does anyone know what the proper regular expression for this would be? 有谁知道这个正确的正则表达式是什么? I should specify I am using Dreamweaver as my editor, which may use different regular expressions. 我应该指定我使用Dreamweaver作为我的编辑器,它可能使用不同的正则表达式。

This expression will match the entire line such that the string "/folder2" occurs in it: 此表达式将匹配整行,以便在其中出现字符串“/ folder2”:

^.+?\/folder2/.+$

HTH. HTH。

In Python that would be: 在Python中将是:

import re
regex = re.compile('.*/folder2/.*')
f = open("filtered_file.txt", "w")
map(lambda x: f.write(x), filter(lambda x: not regex.match(x), open("input.txt")))
f.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM