0

I need to find picture location in HTML text based on regex.

e.g.

The HTML string is:

    <div style='background-image: url(http://www.mydomain.com/images/test.jpg); 
background-repeat: no-repeat; background-attachment: scroll; height: 400px;'>

And I need to define a regex that will find the end location of string that starts with http://www.mydomain.com and finishes with ).

  1. What should be the regex?
  2. How can I find the end location in java?
1
  • Might the http://www.mydomain.com appear elsewhere in the HTML you are parsing? Or can your regex just start with the hard-coded URL and capture the rest? Commented Mar 27, 2012 at 10:32

2 Answers 2

2

I would do something like this to find the url:

String input = "<div style='background-image: url(http://www.mydomain.com/images/test.jpg); \n" +
                "background-repeat: no-repeat; background-attachment: scroll; height: 400px;'>";

Pattern pattern = Pattern.compile("image:\\surl\\(([^)]+)\\)");
Matcher matcher = pattern.matcher(input);
if (matcher.find()){
    String url = matcher.group(1);
    System.out.println(url);
}

or

Pattern pattern = Pattern.compile("image:\\surl\\(http://www\\.mydomain\\.com([^)]+)\\)");

if you want to have only what follows the domain part

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

3 Comments

Then use lastIndex on / to parse it. Then assumes only one image tag in the HTML being parsed.
This is not working for me. Because if I have the following: <div style='background-image: url((mydomain.com/images/test.jpg); color: rgb(112, 47, 43)' > The end location is 3) (from the rgb) and not the end of image ulr
edited regexp to catch string only before first closing ")" after domain name :)
1

Another option would be something like this:

www\\.mydomain\\.com.*/([\\w-\\.]*)

When run on <div style='background-image: url(http://www.mydomain.com/images/test.jpg); background-repeat: no-repeat; background-attachment: scroll; height: 400px;'>

Group # 1 = test.jpg

1 Comment

I tried it as well. <div style='background-image: url(mydomain.com/images/products/HappyBirthday1.jpg);> group1 returned to me div

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.