Reputation: 726
So I have a rather big document with entries like:
"kff126d":"Infinity"
And others like:
"kff126d":"0.03702955"
And a few dozens of other keys like kff126e and so on, and I would like to remove everything that isn't the one key I want to keep, and whatever value is inside the second set of quotation marks, so I can take a look at abnormal values.
Could someone help me out? I've tried looking arround but it's quite complex compared to the few things I've done with regex before this. Thanks in advance.
Upvotes: 1
Views: 5053
Reputation: 626738
I can suggest the following regex:
("kff126d":"[^"]*")|(?:(?!"kff126d":).)+
with .
matches newline option ON, and replace with $1\n
.
See the regex demo
This will insert redundant empty lines that you can remove with Edit > Line Operations > Remove Empty Lines.
The point is to match and capture with (...)
the part that you need to keep, and remove all the rest. All the rest can be matched with a tempered greedy token (?:(?!"kff126d":).)+
(matches any character (with .
) that does not start the "kff126d":
sequence).
And here is the screen of the replacement result with your updated sample data:
Upvotes: 3
Reputation: 15000
Using Notepad++:
Infinity
The Regex
Find What:
.*?(?:"kff126e":"((?!Infinity)[^"]*)"|\Z)
Replace with:
$1\n
From Notepad++
press the ctrlh to enter the find and replace mode
Select the Regular Expression option
Select the ". matches newline" option
In the "Find what" field place the following regex
.*?(?:"kff126e":"((?!Infinity)[^"]*)"|\Z)
In the Replace with field enter $1\n
Click Replace All
Goto pub for favorite beverage.
NODE EXPLANATION
----------------------------------------------------------------------
.*? any character (0 or more times (matching
the least amount possible))
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
"kff126e":" '"kff126e":"'
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
Infinity 'Infinity'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
[^"]* any character except: '"' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\Z before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of grouping
What's happening
The desired value will be placed into capture group 1 aka $1
, which will replace the matched text. The \n
inserts a return character. The regex engine will continue parsing your text after the last replace operation until it reaches the end of the file.
Upvotes: 1
Reputation: 1374
Press ctrl+h and in the find section use : (?m)^(?!"kff126d":).*?$
and leave the replace section blank. Select the search mode 'Regular Expression' and select Replace All.
This will transform
"kff126d":"0.12345"
sdfgsdfg
sgsfgsfg
"kff126d":"0.03702955"
"kff126d":"0.03702955"
rererer
"kff126d":"0.03702955"
sfdgsfgfsg
sdfgsfg
"kff126e":"0.8888888888888"
into
"kff126d":"0.12345"
"kff126d":"0.03702955"
"kff126d":"0.03702955"
"kff126d":"0.03702955"
Afterwards, if you want to remove all the blank lines, you can replace \R+
with \n
Upvotes: 1