亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何匹配給定的正則表達式模式以從字符串中獲取子字符串

如何匹配給定的正則表達式模式以從字符串中獲取子字符串

慕容3067478 2021-12-18 15:55:51
我有一個字符串 ex-“Abc Xyz Def OSSV-1810-017466-AB01 Def Utv” 如何編寫一個正則表達式來對“OSSV-1810-017466-AB01”等格式進行數學運算并僅從任何給定的字符串中提取它?嘗試了什么:public class RegexTest {     public static void main(String[] args) {         String str = "Abc Xyz Def OSSV-1810-017466-AB01 Def Utv";         Pattern pattern = Pattern.compile("[a-zA-Z\\-\\0-9\\-\\0-9\\-\\^A-Za-z0-9]");         Matcher matcher = pattern.matcher(str);         System.out.println(matcher.toString());         if(matcher.find()){             System.out.println("found" + matcher.group(1));         }         else{             System.out.println("not found");         }     } }   
查看完整描述

2 回答

?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

您可以使用這種模式[A-Z]{4}-\\d{4}-\\d{6}-[A-Z]{2}\\d{2}。

matcher.group(1)改為matcher.group(0).


查看完整回答
反對 回復 2021-12-18
?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

public static void main(String[] args) {

    String input="Abc Xyz Def OSSV-1810-017466-AB01 Def Utv";


    String regEx="([a-zA-Z ]*)((?:[A-Z]{4}-)(?:[0-9]{4}-)(?:[0-9]{6}-)(?:[A-Z0-9]{4}))([a-zA-Z ]*)";

    Matcher matcher = Pattern.compile(regEx).matcher(input);            


    if(matcher.matches()) {         

        System.out.println(matcher.group(2));

    }

}


查看完整回答
反對 回復 2021-12-18
  • 2 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號