Re: [問題] 字串多個括號分割
看板RegExp (正規表示式 Regular Expression)作者godspeedlee (妳,我可以)時間13年前 (2012/06/16 00:50)推噓0(0推 0噓 0→)留言0則, 0人參與討論串2/2 (看更多)
※ 引述《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)"
參考看看 :)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.124.97.159
討論串 (同標題文章)
RegExp 近期熱門文章
PTT數位生活區 即時熱門文章