求一高难度正则表达式 要求匹配函数的书写规则:比如a(b,2),a(a(b,2),3)等,支持$1,即比如:a($1,a(b,3));支持嵌套,想了很久都没头绪,那位大哥帮我一下! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java不支持正则的子表达式,没戏 public class Main { /** * @param args */ public static void main(String[] args) { String testString1 = "a(b,2)"; System.out.println(testFunctionIsMatched(testString1)); String testString2 = "a(a(b,2),3)"; System.out.println(testFunctionIsMatched(testString2)); String testString3 = "a($1,a(b,3))"; System.out.println(testFunctionIsMatched(testString3)); String testString4 = "a($1,a(b,3))))"; System.out.println(testFunctionIsMatched(testString4)); } public static final String TEST_PATTERN = "a[(]([$]1|\\d|b),([$]1|\\d|b)[)]"; public static final String LAST_TEST_PATTERN = "^a[(]([$]1|\\d|b),([$]1|\\d|b)[)]$"; public static final String DEFAULT_SUBSTITUTION = "0"; /** * 递归把当前符合基本样式的内容替换成0,直到最后一个测试内容也能满足函数样式 * * @return */ public static boolean testFunctionIsMatched(String testString) { // 打印每次要测试的内容 System.out.println(testString); // 判断是否为最后一个正确的函数样式 if (testString.matches(LAST_TEST_PATTERN)) { return testString.matches(TEST_PATTERN); } // 取得下一个要进行测试的内容 String nextTestString = testString.replaceAll(TEST_PATTERN, DEFAULT_SUBSTITUTION); // 当前的测试内容与下一次测试内容相同时 // 证明以后的测试内容一定无法匹配样式 // 直接返回false if (testString.equals(nextTestString)) { return false; } // 继续后续的测试 return testFunctionIsMatched(nextTestString); }} 很难做到,因为 Java 的正则表达式语法中没有 .net 中的平衡组匹配,无法匹配嵌套结构,只能动态生成指定嵌套深度的正则表达式。希望能从下面的代码中得到启发。import java.util.regex.Matcher;import java.util.regex.Pattern;public class NestedTest { public static void main(String[] args) { String str = "ZZ(BB(DD(EE,AA(FF,OO(ZZ,YY(XX)))),SS(MM,QQ(HH,II(JJ,KK(LL))))))"; for (int i = 0, k = getLevels(str) + 1; i < k; i++) { String regex = generateRegex(i); System.out.println(" Regex: " + regex); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.printf("Level %d: %s%n", i + 1, matcher.group(0)); } System.out.println(); } } /** * Using a dynamic regex to match nested pairs.<br /> * Rewrite program from: Jeffrey E.F.Friedl, Mastering Regular Expressions, * 7.6.1, 3rd ed., O'Reilly, 2006. * * @param level * @return */ private static String generateRegex(int level) { String prefix = "\\(([^()]++"; String suffix = ")*\\)"; String prefixOr = prefix + "|"; if (level <= 0) { return "\\w+" + prefix + suffix; } else { StringBuffer sb = new StringBuffer(prefix + suffix); for (int i = level; i > 0; i--) { sb.insert(0, prefixOr).append(suffix); } return sb.insert(0, "\\w+").toString(); } } private static int getLevels(String str) { char[] c = str.toCharArray(); int leftCount = 0; int rightCount = 0; for (int i = 0; i < c.length; i++) { if (c[i] == '(') { leftCount++; } if (c[i] == ')') { rightCount++; } } if (leftCount == rightCount) { return leftCount / 2; } else { throw new IllegalArgumentException("Nested Level is ERROR!"); } }} 请指教!JAVA 中 判断是不是数字 的两段 代码 有什么区别?? 大家帮小弟看下if语句括号位置不同造成得影响 想获取一个MP3文件最后的128个字节,这样的代码要怎么写? 请教一个奇怪的问题,关于jdbc的 一道题 读取文本时的一个问题 JTree中如何改变节点的背景颜色? 求助!java读取两个txt文件,并分析统计里面的数据后输出 谢谢大家,程序已编译通过,但是解释出现死循环,再线等待,代会结帐!急盼回答! 这段程序为何老是出错,请高手指教 谁有这几个包. 啊啊啊。。请教一个Java的容器/类问题。。。
public class Main { /**
* @param args
*/
public static void main(String[] args) {
String testString1 = "a(b,2)";
System.out.println(testFunctionIsMatched(testString1));
String testString2 = "a(a(b,2),3)";
System.out.println(testFunctionIsMatched(testString2));
String testString3 = "a($1,a(b,3))";
System.out.println(testFunctionIsMatched(testString3));
String testString4 = "a($1,a(b,3))))";
System.out.println(testFunctionIsMatched(testString4));
} public static final String TEST_PATTERN = "a[(]([$]1|\\d|b),([$]1|\\d|b)[)]"; public static final String LAST_TEST_PATTERN = "^a[(]([$]1|\\d|b),([$]1|\\d|b)[)]$"; public static final String DEFAULT_SUBSTITUTION = "0"; /**
* 递归把当前符合基本样式的内容替换成0,直到最后一个测试内容也能满足函数样式
*
* @return
*/
public static boolean testFunctionIsMatched(String testString) {
// 打印每次要测试的内容
System.out.println(testString);
// 判断是否为最后一个正确的函数样式
if (testString.matches(LAST_TEST_PATTERN)) {
return testString.matches(TEST_PATTERN);
}
// 取得下一个要进行测试的内容
String nextTestString = testString.replaceAll(TEST_PATTERN, DEFAULT_SUBSTITUTION);
// 当前的测试内容与下一次测试内容相同时
// 证明以后的测试内容一定无法匹配样式
// 直接返回false
if (testString.equals(nextTestString)) {
return false;
}
// 继续后续的测试
return testFunctionIsMatched(nextTestString);
}
}
import java.util.regex.Pattern;public class NestedTest { public static void main(String[] args) {
String str = "ZZ(BB(DD(EE,AA(FF,OO(ZZ,YY(XX)))),SS(MM,QQ(HH,II(JJ,KK(LL))))))";
for (int i = 0, k = getLevels(str) + 1; i < k; i++) {
String regex = generateRegex(i);
System.out.println(" Regex: " + regex);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.printf("Level %d: %s%n", i + 1, matcher.group(0));
}
System.out.println();
}
} /**
* Using a dynamic regex to match nested pairs.<br />
* Rewrite program from: Jeffrey E.F.Friedl, Mastering Regular Expressions,
* 7.6.1, 3rd ed., O'Reilly, 2006.
*
* @param level
* @return
*/
private static String generateRegex(int level) {
String prefix = "\\(([^()]++";
String suffix = ")*\\)";
String prefixOr = prefix + "|";
if (level <= 0) {
return "\\w+" + prefix + suffix;
} else {
StringBuffer sb = new StringBuffer(prefix + suffix);
for (int i = level; i > 0; i--) {
sb.insert(0, prefixOr).append(suffix);
}
return sb.insert(0, "\\w+").toString();
}
} private static int getLevels(String str) {
char[] c = str.toCharArray();
int leftCount = 0;
int rightCount = 0;
for (int i = 0; i < c.length; i++) {
if (c[i] == '(') {
leftCount++;
}
if (c[i] == ')') {
rightCount++;
}
}
if (leftCount == rightCount) {
return leftCount / 2;
} else {
throw new IllegalArgumentException("Nested Level is ERROR!");
}
}
}