0

I have a very simple problem but I am new to Java Matcher and I am having a hard time figuring out how to use it for my specific problem.

I have a string which is something like this <not needed content>src="url"<not needed content>src="url2"<not needed content>

Where <'not needed content'> are the things I want to ignore in my string. I basically want to extract the URLs from the string.

My code currently looks like this

Pattern MY_PATTERN = Pattern.compile("\\src=\"(.*?)\\\"");
Matcher m = MY_PATTERN.matcher(content);
String s = "something";
while (m.find()) {
   s = m.group(1);
}

I apologize for such basic, and possibly duplicate question.

Thank you.

2
  • I could use an HTML parser, but this is not a full HTML page, this is a small part of an RSS feed which I was hoping to parse without anything fancy. Commented Feb 19, 2013 at 8:19
  • 2
    The beginning of your Pattern "\\src means one whitespace followed by rc. This will never match src as s is not whitespace. Commented Feb 19, 2013 at 8:22

2 Answers 2

2

Why didn't you try a simplier pattern ? Like this one :

Pattern.compile("src=\"(.*?)\"");

(Not tested, but should be better)

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

1 Comment

wow.... that works perfectly, I can't believe I tried so hard. Ill give you answer as soon as I can.
0

You can use either of the following regexes:

src="([^"]+)
src="(.+?"

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.