import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^<tr.*?>(?:<td>.*?<\\/td>){2}<td>(.*?)<\\/td><td>.*?<\\/td><td>(.*?)<\\/td>.*?<a href=\".*?_(.*?)_(.*?)\\.html.*$";
final String string = "<tr><td><small>1</small></td><td>Kalisz</td><td>62-800</td><td>Poland</td><td>Greater Poland</td><td>Kalisz</td><td>Kalisz<tr><td></td><td colspan=6> <a href=\"/maps/browse_51.75_18.087.html\" rel=\"nofollow\"><small>51.75/18.087</small></a></td></tr>\n"
+ "<tr class=\"odd\"><td><small>2</small></td><td>Piotrków Trybunalski</td><td>97-300</td><td>Poland</td><td>Łódź Voivodeship</td><td>Piotrków Trybunalski</td><td>Piotrków Trybunalski<tr class=\"odd\"><td></td><td colspan=6> <a href=\"/maps/browse_51.411_19.689.html\" rel=\"nofollow\"><small>51.411/19.689</small></a></td></tr>\n"
+ "<tr><td><small>3</small></td><td>Toruń</td><td>87-100</td><td>Poland</td><td>Kujawsko-Pomorskie</td><td>Toruń</td><td>Toruń<tr><td></td><td colspan=6> <a href=\"/maps/browse_53.021_18.623.html\" rel=\"nofollow\"><small>53.021/18.623</small></a></td></tr>";
final String subst = "$2;$1;$3\\/$4";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html