import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:(?!<(?:field|exclGroup)(?!\\w)(?>\"[\\S\\s]*?\"|'[\\S\\s]*?'|(?:(?!\\/>)[^>])?)+>)[\\S\\s])*(?><(field|exclGroup)(?=(?:[^>\"']|\"[^\"]*\"|'[^']*')*?\\sname\\s*=\\s*(?:(['\"])([\\S\\s]*?)\\2))\\s+(?>\"[\\S\\s]*?\"|'[\\S\\s]*?'|(?:(?!\\/>)[^>])?)+>)(?:(?&core)|)<\\/\\1\\s*>(?:(?!<(?:field|exclGroup)(?!\\w)(?>\"[\\S\\s]*?\"|'[\\S\\s]*?'|(?:(?!\\/>)[^>])?)+>)[\\S\\s])*(?(DEFINE)(?<core>(?>(?><([\\w:]+)(?>\"[\\S\\s]*?\"|'[\\S\\s]*?'|(?:(?!\\/>)[^>])?)+>)(?:(?&core)|)<\\/\\5\\s*>|(?!<\\/[\\w:]+\\s*>)(?>[\\S\\s]))+))";
final String string = "<field xmlns=\"http://www.xfa.org/schema/xfa-template/2.8/\" y=\"0in\" x=\"0.343mm\" w=\"8.881pt\" h=\"9.108pt\" name=\"detcon_recreation_only\">\n"
+ " <ui>\n"
+ " <checkButton size=\"8.881pt\">\n"
+ " <border>\n"
+ " <edge stroke=\"lowered\"/>\n"
+ " <fill/>\n"
+ " </border>\n"
+ " </checkButton>\n"
+ " </ui>\n"
+ " <font size=\"0pt\" typeface=\"Adobe Pi Std\"/>\n"
+ " <para vAlign=\"middle\"/>\n"
+ " <value>\n"
+ " <text>0</text>\n"
+ " </value>\n"
+ " <items>\n"
+ " <text>1</text>\n"
+ " <text>0</text>\n"
+ " <text/>\n"
+ " </items>\n"
+ "</field>\n"
+ "<field xmlns=\"http://www.xfa.org/schema/xfa-template/2.8/\" name=\"detcon_special_housing\" y=\"5.393mm\" w=\"27.94mm\" h=\"4.134mm\" x=\"0.343mm\">\n"
+ " <ui>\n"
+ " <choiceList>\n"
+ " <border>\n"
+ " <edge stroke=\"lowered\"/>\n"
+ " </border>\n"
+ " <margin/>\n"
+ " </choiceList>\n"
+ " </ui>\n"
+ " <font typeface=\"Arial Narrow\" size=\"6pt\"/>\n"
+ " <margin topInset=\"0mm\" bottomInset=\"0mm\" leftInset=\"0mm\" rightInset=\"0mm\"/>\n"
+ " <para vAlign=\"middle\"/>\n"
+ " <value>\n"
+ " <text>NA</text>\n"
+ " </value>\n"
+ " <items>\n"
+ " <text>Not Applicable</text>\n"
+ " <text>Hotel Component</text>\n"
+ " </items>\n"
+ " <items save=\"1\" presence=\"hidden\">\n"
+ " <text>NA</text>\n"
+ " <text>HC</text>\n"
+ " </items>\n"
+ "</field>\n"
+ "<exclGroup xmlns=\"http://www.xfa.org/schema/xfa-template/2.8/\" name=\"detcon_photo_taken\" x=\"0in\" y=\"0in\">\n"
+ " <?templateDesigner itemValuesSpecified 1?>\n"
+ " <field w=\"12.446mm\" h=\"3.825mm\" name=\"lb_yes\">\n"
+ " <ui>\n"
+ " <checkButton size=\"1.7639mm\" shape=\"round\">\n"
+ " <border>\n"
+ " <?templateDesigner StyleID apcb1?>\n"
+ " <edge/>\n"
+ " <fill/>\n"
+ " </border>\n"
+ " </checkButton>\n"
+ " </ui>\n"
+ " <font typeface=\"Myriad Pro\"/>\n"
+ " <margin leftInset=\"1mm\" rightInset=\"1mm\"/>\n"
+ " <para vAlign=\"middle\"/>\n"
+ " <caption placement=\"right\" reserve=\"7.698mm\">\n"
+ " <para vAlign=\"middle\" spaceAbove=\"0pt\" spaceBelow=\"0pt\" textIndent=\"0pt\" marginLeft=\"0pt\" marginRight=\"0pt\"/>\n"
+ " <font size=\"8pt\" typeface=\"Arial Narrow\" baselineShift=\"0pt\"/>\n"
+ " <value>\n"
+ " <text>YES</text>\n"
+ " </value>\n"
+ " </caption>\n"
+ " <value>\n"
+ " <text xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>\n"
+ " </value>\n"
+ " <items>\n"
+ " <text>1</text>\n"
+ " </items>\n"
+ " </field>\n"
+ " <field w=\"28.702mm\" h=\"3.825mm\" name=\"lb_no\" x=\"13.233mm\">\n"
+ " <ui>\n"
+ " <checkButton size=\"1.7639mm\" shape=\"round\">\n"
+ " <border>\n"
+ " <?templateDesigner StyleID apcb1?>\n"
+ " <edge/>\n"
+ " <fill/>\n"
+ " </border>\n"
+ " </checkButton>\n"
+ " </ui>\n"
+ " <font typeface=\"Myriad Pro\"/>\n"
+ " <margin leftInset=\"1mm\" rightInset=\"1mm\"/>\n"
+ " <para vAlign=\"middle\"/>\n"
+ " <caption placement=\"right\" reserve=\"23.954mm\">\n"
+ " <para vAlign=\"middle\" spaceAbove=\"0pt\" spaceBelow=\"0pt\" textIndent=\"0pt\" marginLeft=\"0pt\" marginRight=\"0pt\"/>\n"
+ " <font size=\"8pt\" typeface=\"Arial Narrow\" baselineShift=\"0pt\"/>\n"
+ " <value>\n"
+ " <text>NO (see comments)</text>\n"
+ " </value>\n"
+ " </caption>\n"
+ " <value>\n"
+ " <text xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>\n"
+ " </value>\n"
+ " <items>\n"
+ " <text>0</text>\n"
+ " </items>\n"
+ " </field>\n"
+ " <border>\n"
+ " <edge presence=\"hidden\"/>\n"
+ " </border>\n"
+ " <?templateDesigner expand 1?></exclGroup>";
final String subst = "VALUES (12345,\"$3\",\"/Tx\", \"\", \"N\", \"Y\"),\\r\\n";
final Pattern pattern = Pattern.compile(regex);
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