Phate
Phate

Reputation: 6612

Extract src image field from a simple html string without a parser

I have html strings like these:

<img src="mysrc" width="128" height="92" border="0" alt="alt" /><p><strong>...

I'd like to extract mysrc. I don't want to use an html parser as I'll just have to deal with simple html strings...is there an efficient way to extract source field just with string/regular expressions? Or maybe with android default xml parser?

Upvotes: 1

Views: 3678

Answers (2)

Kent
Kent

Reputation: 195129

regex you could try : "(?<=<img src=\")[^\"]*"

example:

@Test
    public void testX() {
        final String s = "<img src=\"mysrc\" width=\"128\" height=\"92\"...";
        final String regex = "(?<=<img src=\")[^\"]*";
        final Pattern p = Pattern.compile(regex);
        final Matcher m = p.matcher(s);
        while (m.find()) {
            System.out.println(m.group());
        }

    }

will give you

mysrc

Upvotes: 3

MadProgrammer
MadProgrammer

Reputation: 347244

You could do some thing like

String text = "<img src=\"mysrc\" width=\"128\" height=\"92\" border=\"0\" alt=\"alt\" /><p><strong>";

text = text.substring(text.indexOf("src=\""));
text = text.substring("src=\"".length());
text = text.substring(0, text.indexOf("\""));
System.out.println(text);

Effective, probably, efficient, probably not so much

Upvotes: 1

Related Questions