import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<(\\S+) tag=\"([^\"]+)\">([^<]+)<\\/(?1)>";
final String string = "<image tag=\"plan\">http://img.nmarket.pro/photo/pid/E2087273-BDC1-4717-9F2E-20F6A76E0479/?v=1&cid=2415&pid=E2087273-BDC1-4717-9F2E-20F6A76E0479&wpsid=51&hash=031b3173e71427e3673247f8a80135c8</image>\n"
+ " <image tag=\"houseuncategorized\">http://img.nmarket.pro/photo/pid/1C372F09-3440-49FE-9A6D-048EF51B5F73/?v=1&cid=2415&pid=1C372F09-3440-49FE-9A6D-048EF51B5F73&wpsid=51&hash=810aea88a716dfff20ae5c182bf38888</image>\n"
+ " <image tag=\"houseuncategorized\">http://img.nmarket.pro/photo/pid/3628BB57-3D4C-43A9-AD00-843E5D5F657F/?v=1&cid=2415&pid=3628BB57-3D4C-43A9-AD00-843E5D5F657F&wpsid=51&hash=abf93d04775994f55fe82eb84dfad54e</image>\n"
+ " <image tag=\"houseuncategorized\">http://img.nmarket.pro/photo/pid/3EDBD70B-7393-4A93-9BAD-54B32DA49EEF/?v=1&cid=2415&pid=3EDBD70B-7393-4A93-9BAD-54B32DA49EEF&wpsid=51&hash=0565037ec9d45e15d0c84df34100e3dc</image>\n"
+ " <image tag=\"houseuncategorized\">http://img.nmarket.pro/photo/pid/2C8FE09E-AE07-4E13-89F0-B4695A7428AF/?v=1&cid=2415&pid=2C8FE09E-AE07-4E13-89F0-B4695A7428AF&wpsid=51&hash=f1a03336866c285fb16a604708a9c15e</image>\n"
+ " <image tag=\"housemain\">http://img.nmarket.pro/photo/pid/0CCFA7EA-3757-4740-A721-BF75DEAAB11C/?v=1&cid=2415&pid=0CCFA7EA-3757-4740-A721-BF75DEAAB11C&wpsid=51&hash=c69a5cf80cd48f6ccc4b68c077d5f301</image>\n"
+ " <image tag=\"complexscheme\">http://img.nmarket.pro/photo/pid/29E51673-697F-4F38-9F9B-1DF28B22F3A1/?v=1&cid=2415&pid=29E51673-697F-4F38-9F9B-1DF28B22F3A1&wpsid=51&hash=c826f221b1d372d0defbc04c857f01f3</image>\n"
+ " <image tag=\"floorplan\">http://img.nmarket.pro/photo/pid/F0DB967A-895F-4740-9FFE-FF3F100519EE/?v=1&cid=2415&pid=F0DB967A-895F-4740-9FFE-FF3F100519EE&wpsid=51&hash=15c82a24c7c0c6bacd8b1551c44ca0ec</image>\n"
+ "<parking tag=\"hello\">world</class>\n"
+ " ";
final String subst = "<$1><$2>$3</$2></$1>";
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