import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?P<macroName>\\[\\w+\\s)(?#匹配宏名称)|(?P<paramName>\\w+(?=\\=))(?#匹配参数名称)|(?P<intValue>(?<=\\=)\\d+)(?#匹配int参数)|(?P<intArrayValue2>(?<=\\=)\\[\\d+,\\d+\\])(?#匹配长度为2的int数组)|(?P<intArrayValue4>(?<=\\=)\\[\\d+,\\d+,\\d+,\\d+])(?#匹配长度为4的int数组)|(?P<stringValue1>\\\"[a-zA-Z\\._0-9\\s]+\\\")(?#匹配使用双引号的string参数)|(?P<stringValue2>\\'[a-zA-Z\\._0-9\\s]+\\')(?#匹配使用单引号的string参数)|(?P<var>(?<=\\=)\\w+)(?#匹配变量型参数)";
final String string = "//完整代码匹配测试\n"
+ "[sprite file=\"1.png\" index=2 rect=[20,20,800,600] pos=[800,600]][sprite file='2.png' index=2 rect=[0,0,0,0]] //这是注释 abcd\n"
+ "[sprite file='2.png' index=2 target=basic_layer]\n"
+ "//遇到这种情况,利用python取出组的名称,判断字符串格式 endwith\n"
+ "[sprite pos=[x,y]] [sprite pos=[3,4]]";
final String subst = "";
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