import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^\\h*\n"
+ "(?<linetype>[0123456])\\h+\n"
+ "(?<color> (?&Number) )\\h+\n"
+ "(?<xrot> (?&Number) )\\h+ (?<yrot> (?&Number) )\\h+ (?<zrot> (?&Number) )\\h+\n"
+ "(?<xpos> (?&Number) )\\h+ (?<ypos> (?&Number) )\\h+ (?<zpos> (?&Number) )\\h+\n"
+ "(?<xcam> (?&Number) )\\h+ (?<ycam> (?&Number) )\\h+ (?<zcam> (?&Number) )\\h+\n"
+ "(?<xsca> (?&Number) )\\h+ (?<ysca> (?&Number) )\\h+ (?<zsca> (?&Number) )\\h+\n"
+ "(?<filename>\\S+[.]\\S+)\n"
+ "$\n\n"
+ "(?(DEFINE)\n"
+ " (?<Number> [-]? \\d+ (?> [.]\\d* )? (?> [Ee] [+-]? \\d+ )? )\n"
+ ")";
final String string = "1 0 0 0 0 1 0 0 0 1 0 0 0 1 3024.dat\n"
+ "#linetype color XROT YROY ZROT XPOS YPOS ZPOS XCAM YCAM ZCAM XSCA YSCA ZSCA FILE";
final String subst = "${linetype} ${color} ($xrot,$yrot,$zrot) {$xpos,$ypos,$zpos} <$xcam,$ycam,$zcam> [$xsca,$ysca,$zsca] \"$filename\"";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS);
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