I have a Java String variable containing HTML in which I want to replace all the names of PNG images by another name.
Example input HTML
<html>
<head>
<link rel="stylesheet" media="screen" href="style.css"/>
</head>
<body>
<img href="test1.png" />
<img href="test2.png" />
</body>
</html>
Typical output HTML should be
<html>
<head>
<link rel="stylesheet" media="screen" href="style.css"/>
</head>
<body>
<img href="C:\foo\bar\test1.png" />
<img href="C:\foo\bar\test2.png" />
</body>
</html>
Currently I have this Java code that provides me the new name by loading the image as a ressource. However I can't find the good regex to select all (and only) the images names (with extension but without quotes), can anyone help me on that ?
Pattern imagePattern = Pattern.compile(" TODO ");
Matcher imageMatcher = imagePattern.matcher(taskHTML);
while (imageMatcher.find())
{
String oldName = imageMatcher.group(1);
String newName = "" + getClass().getResource("/images/" + imageMatcher.group(1));
taskHTML.replace(oldName, newName);
}
The matcher should list the following elements:
[test1.png, test2.png]