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