问题
Is there any way I can replace all the words/lines which don't match in my search query in text editors like notepad++ or sublime text.
For example I have a document having few url links in it. Can I do something which leaves only url links in my document. If I have to remove url links, I can search them using regex and replace them with an empty string. But can I do the same thing but for the content which doesn't match regex.
Example:
this is line which I want to remove and can also have special characters in it link % $ [] (0) and here is url: https://google.com one more line with some random garbagee and https://www.example.com
For above text, output should be:
https://google.com
https://www.example.com
回答1:
In Sublime Text, you can search, hit "Find All", then copy and paste all the matches at once into a new document. This isn't exactly "negative search", but it does accomplish your goal.
回答2:
With Notepad++ you can do it in two passes. The first pass isolates the wanted text. The second pass removes the unwanted pieces.
Firstly do a regular expression search for \b(https://[^\s]+)(\s|$)
and replace with \r\n\1\r\n
. This is a very crude and easy to fool URL detector, but it works on the examples you give. The search string looks for "https://" preceded by a word boundary (ie \b
). That is followed by some non-whitespace characters that are considered to be part of the wanted text. The last part of the search text looks for either a whitespace character or the end of line. The wanted part is retained in a capture for the replace text.
Second do a regular search for ^https://
using the "Mark" tab in the find window. Select "Bookmark lines" then click on "Mark all". (You might like to click on "Clear all marks" before clicking on "Mark all".) Finally use menu => Search => Bookmark => Remove unmarked lines.
(Checked in Notepad++ version 6.6.9)
回答3:
In SynWrite app:
- call dialog "Search/ Extract strings"
- enter Regex for URL, do "Find"
- now press button to copy found URLs to new tab
来源:https://stackoverflow.com/questions/27481078/how-to-perform-negative-search-or-replace-in-common-text-editors