这里的一些Java代码应该做你想要的东西:
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^-?([^\\.]+\\.){1,2}\\((\\d+-\\d+|\\d+)|([^|]+|[^|]+)\\)$");
String[] samples = { "world.block.(1-2|1).(test1|stack)",
"test.test.(1-2|1)",
"world.(test1|stack)",
"-help.block.(1-2|1).(test1|stack)",
"-world.help",
"world.stack",
"world.block."
};
for (String sample : samples) {
Matcher matcher = pattern.matcher(sample);
System.out.println(sample + (matcher.find() ? " matched" : " failed"));
}
}
输出是:
world.block.(1-2|1).(test1|stack) matched
test.test.(1-2|1) matched
world.(test1|stack) matched
-help.block.(1-2|1).(test1|stack) matched
-world.help failed
world.stack failed
world.block. failed
唯一缺少的是范围检查的数字。可以添加它,但我宁愿使用匹配的组,并使用数学进行范围检查,以避免过分复杂的正则表达式。
不清楚,你的输入是什么,你试图匹配什么? – Dalorzo 2014-09-03 18:20:39
我想你忘了告诉我们你需要什么正则表达式来匹配 – 2014-09-03 18:22:14
,试试这个正则表达式:'(world \ .block \。\(1-2 \ | 1 \)\。\(test1 \ | stack \))|(-world \ .block \ \(1-2 \ | 1 \)\ \(TEST1 \ |。栈\)|。(世界\ .block \ \(1-2 \ | 1 \) ))|(world \。\(test1 \ | stack \))' – 2014-09-03 18:24:16