Re: [J2SE] 可以利用regexp來parse數字嗎
※ 引述《machican (麻糬可以的,麻糬罐)》之銘言:
: 請問一下 regular expression 可以用來輔助 parse 字串嗎?
: 因為我今天想要辨識一段這樣的字串: \d+[[a-zA-Z_0-9]+\d+]*
: 然後將它裡面的數字都抓出來,中間用來 split 的字串不一定是什麼東西
: 也會含有符號甚至是一些invisible character
: 除了自己硬幹去抓數字出來parse之外,請問一下有沒有比較方便的方法呢?
: 我在幻想有沒有自動return那堆\d+成一個字串陣列的功能
: Ps. 在Pattern class中split功能似乎必須先知道分隔字串的規則
: 現在我想要的是相反的,我只知道我要的內容的規則,想要把它割出來
單純就這個狀況,可以用Matcher,
split單純就是切割,而Matcher是比對。
sample code:
Pattern p=Pattern.compile("\\d+[[a-zA-Z_0-9]+\\d+]*");
Matcher m=p.matcher("the word to match 1 asdasd 34234 ");
while(m.find()){
System.out.println("found:["+m.group()+"]");
}
/*ps.一次find會移動一次找到的位置。 */
--
I am a person, and I am always thinking .
Thinking in love , Thinking in life ,
Thinking in why , Thinking in worth.
I can't believe any of what ,
I am just thinking then thinking ,
but worst of all , most of mine is thinking not actioning...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.134.27.68
推
04/10 11:17, , 1F
04/10 11:17, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):