Re: [问题] 字串多个括号分割

楼主: godspeedlee (妳,我可以)   2012-06-16 00:49:58
※ 引述《blueshika (玺佧)》之铭言:
: 输入的字串是这个样子,目的是要分别取出括号如(aaa,aaa,123)
: test((aaa,aaa,123)(bbb,bbbb)(ccc,cc,7891)...)
: 我自己写了如下的规则
: (\([a-z]+\,[a-z]+\,*\d*\))
: 我在这个网页测试http://osteele.com/tools/rework/
: 结果如下
: results[0] = "test("
: results[1] = "(aaa,aaa,111)"
: results[2] = empty string
: results[3] = "(bbb,bbbb)"
: results[4] = empty string
: results[5] = "(ccc,cc)"
: results[6] = ")"
: 可是我把规则写在java里面执行
: private static String regex = "(\\([a-z]+\\,[a-z]+\\,*\\d*\\))";
: Pattern p = Pattern.compile(regex);
: String[] array = p.split(input_string);
: 结果却是
: 第0个:test(
: 第1个:
: 第2个:
: 第3个:)
: 请问是哪里出错??
: 还有我一开始的规则这样写OK吗?
提供另一个解法:
Pattern: \(\w+(,\w+)*\)
Matches:
match[0] = "(aaa,aaa,123)"
match[1] = "(bbb,bbbb)"
match[2] = "(ccc,cc,7891)"
参考看看 :)

Links booklink

Contact Us: admin [ a t ] ucptt.com