Delete everything except a specific pattern.

I have a log file with lots of lines. I would like to delete everything from this file (find and replace), with the exception of any line that starts with: phone= and ends with Digits=1

for example: phone=97212345678&step=1&digits=1

To find this line, I use (phone=.*digits=1) and it works! but I could not find the regex to select everything except this line and clear them all.

sample file .

+10
source share
4 answers

To delete anything other than a specific text, you need to use .*(text_you_need_to_keep).* corresponding to the new line.

In Notepad ++ use

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

NOTE :. matches the newline option .

Am I using \S*? instead of .* inside the capture template, since you only want to combine any characters without spaces as little as possible from phone= to the nearest digits . .* too greedy and can stretch over several lines with the DOTALL option.

UPDATE

If you want to save several multiple occurrences of a template in text, in Notepad ++, you can use

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

Replace with $1\n . In this case, you will delete all unwanted substrings, except those that were after the last occurrence of your required subpattern.

You will need to remove the last fragment either manaully or

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

If you use some tools like Notepad ++ or EditPlus, you can use the following regex replacement:

Find the line: ^phone=(\d+&step=1&)digits=1

Replace string: \1

+1
source

Regex to find a match:

.

/ ^ phone = + & numbers = 1 $ /

To replace a file, besides matching:

/ ^ (?! phone = + &. Digits = 1 $). * / G

0
source

Let's say you have data like:

"for the implementation plan [ID = 7420] on 12/06/2018 08:00:00"

you want to extract only a part of [ID = dddd] from thousands of lines. In Notepad ++, press ctrl + h open replace window, check the regex.

Find what:

 .*?(\[ID = \d+\]).* 

Replace:

 \1 
0
source

All Articles