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

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

如何在Java中的String.contains()方法中使用正則表達式

如何在Java中的String.contains()方法中使用正則表達式

慕碼人8056858 2019-10-30 14:43:42
我想檢查一個字符串是否按順序包含單詞“ stores”,“ store”和“ product”。無論它們之間是什么。我嘗試使用someString.contains(stores%store%product);并且.contains("stores%store%product");我是否需要顯式聲明一個正則表達式并將其傳遞給方法,還是根本無法傳遞正則表達式?
查看完整描述

3 回答

?
瀟瀟雨雨

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

public static void main(String[] args) {

    String test = "something hear - to - find some to or tows";

    System.out.println("1.result: " + contains("- to -( \\w+) som", test, null));

    System.out.println("2.result: " + contains("- to -( \\w+) som", test, 5));

}

static boolean contains(String pattern, String text, Integer fromIndex){

    if(fromIndex != null && fromIndex < text.length())

        return Pattern.compile(pattern).matcher(text).find();


    return Pattern.compile(pattern).matcher(text).find();

}

1.結果:正確


2.結果:正確


查看完整回答
反對 回復 2019-10-30
  • 3 回答
  • 0 關注
  • 7630 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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