Replace content between string that contains a specific word
-
hi,i have this sentence, but not all the time is like this only i know is between },{
},{“host”:“ok”,“type”:“sub”,“url”:“https:// ok. ru/videoembed/7335889799713”},{
and i want to replace or delete everything that is between },{random},{ while contains ok. ru
is there any way to do it in notepad++?
thank you -
First, right off the bat, understand that parsing and editing JSON with regular expressions is a bad idea, and is likely to fail. See our FAQ about the subject.
To show why it’s so difficult, using test data of the following, so that the example has two that match with one in between
},{"host":"ok","type":"sub","url":"https:// ok. ru/videoembed/7335889799713"},{},"host":"ok","type":"sub","url":"https:// ko. ur/videoembed/7335889799713"},{},"host":"ok","type":"sub","url":"https:// ok. ru/videoembed/7335889799713"},{
The first thing you need to know is that in Regular Expression mode,
.*
matches one or more of “any character” (except newline, if you don’t have the checkbox marked), so that’s going to be the basis for your “random”. Unfortunately, you also need to exclude the.*
matching an},{
, otherwise, if you have data like the three I showed, you might end up with the replacement going across multiple},{...},{
sequences. This can happen even if you use the?
modifier as.*?
to try to capture as little as possible.With the reasonable guess of
},{.*?ok\. ru.*?},{
to try to match that sequence that hasok. ru
in the middle, it properly matches the first, but the second match is too big, because .?* doesn’t actually restrict it from matching},{
in the middle:
If the “random” cannot contain other
{
or}
characters (ie, if your JSON is not nested), then you can just use a character class instead of . – I would suggest[^\r\n{}]
, which says “any character that is not a newline or a brace character”, So using
FIND =},{[^\r\n{}]*?ok\. ru[^\r\n{}]*?},{
and REPLACE =},{},{
with SEARCH MODE=Regular Expression works reasonably on my example data above, resulting in:},{},{},{"host":"ok","type":"sub","url":"https:// ko. ur/videoembed/7335889799713"},{},{},{
But if your JSON is any more complicated, that regex will fail.
At that point, I would recommend investigating the JsonTools plugin, which has JSON-aware search-and-replace, which might be able to handle your needs, better handling edge cases. I don’t know enough about that plugin’s specifics to craft an example for you… but you might be able to use the ideas I’ve given plus the info in the FAQ to move forward. If you have specific questions, there are users here, including that FAQ’s author, who might be able to answer specific questions, after you’ve given the JsonTools plugin a try on your own first.
----
Useful References
-
@PeterJones
i cant make it with the plugin also, and my line is like},{\ "host\ ":\ "ok\ ",\ "type\ ":\ "sub\ ",\ "url\ ":\ "https:// ok. ru/videoembed/7335889799713\ "},{
without the spaces
thank you, ill try to find another way or tools if i cant here
-
I’m the JsonTools maintainer.
Pretty sure that with JsonTools, if you have an array of objects, and you want to select only those objects where theurl
field containsok.ru
, you can just run the query@[@[:].url =~ `ok\.ru`]
.
I don’t have a computer handy, so I can’t be of more than limited assistance. -
or at least is there an way to find all sentence like:
{\ "host\ ":\ "ok\ ",\ "type\ ":\ "sub\ ",\ "url\ ":\ "https:// ok. ru/videoembed/7335889799713\ "},
i try {\ "host\ ":\ "ok\ ",\ "type\ ":\ "sub\ ",\ "url\ ":\ "https:// ok. ru/videoembed/([0-9]*)\ "}, but is not working also
-
Please, please read Formatting Forum Posts, as I linked to you before. Your example data and regexes are not coming across as I believe you intend, which makes your questions/replies essentially unintelligible.
On your first post, I could guess what you intended. But the last few have been indecipherable to me. As such, I cannot help you anymore, and anyone else who tries will just be guessing what you actually meant.
-
@PeterJones im asking the same thing, but if i cant get the sentence between {…} at least i want to find and replace something like {\ "host\ ":\ "ok\ ",\ "type\ ":\ "sub\ ",\ "url\ ":\ "https:// ok. ru/videoembed/random number\ "} and the problem is the \ (slash) when i try to use expressions
-
im asking the same thing
I cannot believe your text has all the backslashes that we see in your post:
Does your text really have all those backslashes? Because your original post did not have all those backslashes, but your followon text does. If your real text does not, then read the FAQ on how to format your question. If you want an answer, format your text so that it acurately reflects the text you have. We cannot help you if you don’t share the text you actually have.
-
@PeterJones yes it has all the backslashes, in the first post it didn’t had cuz the editor here hide it, and i saw it after i post, and i have to add space after every backslashes so you can see it
-
So there are really backslashes but not spaces? That means your post is still not showing the actual data? All you have to do to show us the real data is to hit the
</>
button on your post’s toolbar and paste the data in between. It’s really quite simple to do, and reading the FAQ would have shown you how days ago.We still cannot help you until you show your actual data.
-
{\"host\":\"ok\",\"type\":\"sub\",\"url\":\"https://ok.ru/videoembed/7335889799713\"}
this is it
-
So if I’ve understood correctly, the text here has two copies of what you don’t want and two copies that would be okay to keep
{\"host\":\"ok\",\"type\":\"sub\",\"url\":\"https://something.else/videoembed/7335889799713\"},{\"host\":\"ok\",\"type\":\"sub\",\"url\":\"https://ok.ru/videoembed/7335889799713\"},{\"host\":\"ok\",\"type\":\"sub\",\"url\":\"https://something.else/videoembed/7335889799713\"},{\"host\":\"ok\",\"type\":\"sub\",\"url\":\"https://ok.ru/videoembed/7335889799713\"}
If you want to turn the
ok.ru
versions into just{}
, then just
FIND ={[^}]*ok\.ru[^}]*}
REPLACE ={}
That will find any
{...}
that has the literalok.ru
in it, and turn it into just{}
, and it shouldn’t ever grab too much. It will end up with{\"host\":\"ok\",\"type\":\"sub\",\"url\":\"https://something.else/videoembed/7335889799713\"},{},{\"host\":\"ok\",\"type\":\"sub\",\"url\":\"https://something.else/videoembed/7335889799713\"},{}
If your data is ever much different from the example you showed, I make no guarantees.
Good luck.
-
@PeterJones it is working good, thank you much