您好,登錄后才能下訂單哦!
怎么在Java中利用正則表達(dá)式對(duì)字符串進(jìn)行處理?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
Java是一門面向?qū)ο缶幊陶Z言,可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。
代碼如下所示:
package java_test; import java.util.regex.Matcher; import java.util.regex.Pattern; class Main { public static void main(String[] args) { String lineString = "[\"1\"]"; String line = "[\"on\",\"1\",\"5\",\"8\",\"10\"]"; lineString = line.replaceAll("[\"\\[\\]]", "");//用""替換" [ ] String[] word = lineString.split(","); //以,切割 System.out.println(lineString); for(int i=0;i<word.length;i++){ Pattern pattern = Pattern.compile("[0-9]*"); //正則,匹配數(shù)字 Matcher matcher = pattern.matcher(word[i]); if(matcher.matches()){ System.out.println("1:可以轉(zhuǎn)換"); System.out.println(Integer.parseInt(word[i])); } else { System.out.println("2:不能轉(zhuǎn)換"); System.out.println(word[i]); } } } }
關(guān)于怎么在Java中利用正則表達(dá)式對(duì)字符串進(jìn)行處理問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。