簡體   English   中英

使用正則表達式查找子字符串之一是否存在於不帶空格的字符串中

[英]Find if one of substrings is present in a string without spaces using regex

我想使用Java中的正則表達式檢查字符串中是否存在多個子字符串之一。

SomeString: randomtextrickmortysummergazorpazorp
Output needed: true

我嘗試使用以下模式,但返回false

String patternString = "rick|morty";
String text = "randomtextrickmortysummergazorpazorp";

Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);

System.out.println("Matches :" + matcher.matches());

請幫助。

這是因為,如果整個字符串與您的模式匹配,則matches()僅返回true 使用find()代替:

matchs()

嘗試根據圖案匹配整個區域。

System.out.println("Found :" + matcher.find());

find()

嘗試找到與模式匹配的輸入序列的下一個子序列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM