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. I need a regular expression to perform a find and replace for everything that uses /folder2/
and delete those lines from my list. So find/replace it with blank.
Doe开发者_C百科s 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.
This expression will match the entire line such that the string "/folder2" occurs in it:
^.+?\/folder2/.+$
HTH.
In Python that would be:
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()
精彩评论