溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Java中利用正則表達(dá)式對(duì)字符串進(jìn)行處理

發(fā)布時(shí)間:2021-02-22 15:38:14 來源:億速云 閱讀:152 作者:Leah 欄目:互聯(lián)網(wǎng)科技

怎么在Java中利用正則表達(dá)式對(duì)字符串進(jìn)行處理?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

Java是什么

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í)。

向AI問一下細(xì)節(jié)

免責(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)容。

AI