0

I need a regex to extract server names from below json based on the path. So ideally I should get server1 & server2 the string between https & /upload/image/app as output and ignore the youtube url.

{
    "url1" : "https://server1/upload/image/app/test1.jpg",
    "video" : "https://www.youtube.com/watch?v=K7QaD3l1yQA",
    "type" : "youtube",
    "url2" : "https://server2/upload/image/app/test2.jpg"
}

Tried this, but i know this wont work:

https://(.*?)/upload/image/app
5
  • 1
    What is the question here? You said it yourself: use a regex to extract the informaiton from the string you get when iterating over the entries... Commented Mar 27, 2015 at 13:32
  • Not able to formulate a regex that does this, the youtube url is making it difficult Commented Mar 27, 2015 at 13:35
  • In what programming language/environment are you planning to do this? Commented Mar 27, 2015 at 13:35
  • Then post your attempts so far and point out what is not working. How else should we help with that otherwise? Commented Mar 27, 2015 at 13:36
  • Updated my original question. Commented Mar 27, 2015 at 13:38

2 Answers 2

1
^(?:http|https)\:\/\/(.*?)\/(?:.*?)$

This should do the trick. Examples:

<?php
preg_match("/^(?:http|https)\:\/\/(.*?)\/(?:.*?)$/", "https://server1/upload/image/app/test1.jpg", $matches);
echo $matches[1]; //server1
?>

It's not so difficult to work with regex, I suggest you to start learining at least basics because they may be useful

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

8 Comments

My input is not a single URL but the whole json
I'm not here to do your homework. You asked for a regex and now you have it
Well the regex changes if you use single url vs the whole json, thanks for the help anyways.
Can't you parse the JSON and then match for all elements?
@Farhan Sounds like a terrible idea. Don't take lazy shortcuts like this. This leads to bad code and maintenance issues down the road. Also, regular expression engines are NOT parsers, so don't use them as such. Instead you can combine the technologies to reach your goal.
|
0

You can try with something like that:

^.*http.*//(\S+)/upload/ima.*

Edit: including a JAVA example:

Pattern p = Pattern.compile("^.*http.*//(\\S+)/upload/ima.*");
Matcher m = p.matcher(nextLine);

if (m.find()) {
    System.out.println(m.group(1));
}

Check: Using Regular Expressions to Extract a Value in Java

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.