3

I want to make a regex so I can do a "Search/Replace" over a json file with many object. Every object has a key named "resource" containing a URL.

Take a look at these examples:

"resource":"http://www.img/qwer/123/image.jpg"

"resource":"io.nl.info/221/elephant.gif"

"resource":"simgur.com/icon.png"

I want to make a regex to replace the whole url with a string like this: img/filename.format.

This way, the result would be:

"resource":"img/image.jpg"

"resource":"img/elephant.gif"

"resource":"img/icon.png"

I'm just starting with regular expressions and I'm completely lost. I was thinking that one valid idea would be to write something starting with this pattern "resource":"

and ending with the last five characters. But I don't even know how to try that.

How could I write the regular expression?

Thanks in advance!

3 Answers 3

13

Try this:

Find: "resource":\s*"[^"]+?([^\/"]+)"
Replace: "resource":"img/\1

Using [^"]+? ensures the match won't roll off the end of the current entry and gobble up too much input, and it's reluctant (with the added ?) so it gets the whole image file name (instead ofwhat the last character).

Edit:

I added optional whitespace after the key, which your pastebin has.

See a live demo of this regex with your pastebin.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks Bohemian! There is something I don't get. When I try this in a file with one line like this: "resource":"img/qwer/123/image.jpg", then it works, but when I try it with a larger string like the one from this link pastebin.com/DVQSXJ5t there are no matches. Do you know what is happening?
I've improved the regex - try it now
thanks for the explanation Behemian! I tried the new regex but my text editor still says: "Can't find the text" with the larger string. What could be wrong?
5

Regex

.*\/

Regular expression visualization

Debuggex Demo

This will find the text you want to replace. Replace it with img/ if you want to find the whole text you'll need to look for the following Regex:

("resource":").*\/

Regular expression visualization

Debuggex Demo

Then replace with $1img/ this should give you group 1 and the img part.

Let me know if there are any questions

Note: I personally would just use objects since you have the JSON and parse it to a object then iterate over the objects and change each resource on each object independently rather than looking for a magic bullet

2 Comments

This won't work except for the most trivial of cases due to the greedy quantifier
@Bohemian completely agree, hints my note. :) I could make it more useful i suppose.
2

If your JSON is an array of objects containing resource field I would do it in 3 steps: convert to object, find resources and replace them, convert back to string (optional)

var tmp = JSON.parse('<your json>');
for (i = 0; i < tmp.length; ++i) {
    for (e in tmp[i]) 
        if (e == 'resource') 
            tmp[i][e] = tmp[i][e].replace(/.*(?=img\/.*\..*)/,'') 
}
tmp = JSON.stringify(tmp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.