java - How can I extract all substring by matching a regular expression? -
i want extract values of src attribute in string, how can that:
<p>test <img alt="70" width="70" height="50" src="/adminpanel/userfiles/image/1.jpg" /> test <img alt="70" width="70" height="50" src="/adminpanel/userfiles/image/2.jpg" /> </p>
here go:
string data = "<p>test \n" + "<img alt=\"70\" width=\"70\" height=\"50\" src=\"/adminpanel/userfiles/image/1.jpg\" />\n" + "test \n" + "<img alt=\"70\" width=\"70\" height=\"50\" src=\"/adminpanel/userfiles/image/2.jpg\" />\n" + "</p>"; pattern p0 = pattern.compile("src=\"([^\"]+)\""); matcher m = p0.matcher(data); while (m.find()) { system.out.printf("found: %s%n", m.group(1)); }
most regex flavors have shorthand grabbing matches, ruby's scan
method or .net's matches()
, in java have spell out.
Comments
Post a Comment