我有以下文字:
&rule_c(2-7, <<'EOF');
cout << "Hello World.\n";
return x;
EOF
我想将此文本匹配为正则表达式。
我正在考虑的一个是:
^&rule_c\((\d+)\-(\d+),\s?\<\<\s?\'EOF\'\);\r?\n|\r\n?(.*\r?\n|\r\n?)+EOF\r?\n|\r\n?$
我用Java试过:
private static final String newLine = System.getProperty("line.separator").toString();
...
String textual = "&rule_c(2-7, <<'EOF');" + newLine
+ "cout << "Hello World.\n";" + newLine
+ "return x;" + newLine
+ "EOF" + newLine;
String lineSep = "\\r?\\n|\\r\\n?";
String regex = "^&rule_c\\((\\d+)\\-(\\d+),\\s?\\<\\<\\s?\\'EOF\\'\\);"
+ lineSep + "(.*" + lineSep + ")+EOF" + lineSep + "$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(textual);
if (m.matches()) {
rangeLowerBound = Integer.parseInt(m.group(1));
rangeUpperBound = Integer.parseInt(m.group(2));
String[] tmp = m.group(3).split(lineSep);
System.out.println(tmp.toString());
for (String l : tmp)
System.out.println(l);
lineSet = new ArrayList<String>();
Collections.addAll(lineSet, tmp);
} else
System.out.println("regex doesn't match!");
...
我得到的唯一结果是regex doesn't match!
.
我哪里失败了?