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

如何在Java中的String.contains()方法中使用正则表达式,java,regex,string,Java,Regex,String,我想检查字符串是否按顺序包含单词“stores”、“store”和“product”,无论它们之间是什么 我尝试使用someString.contains(存储%store%产品)和包含(“存储%store%产品”) 我是否需要显式声明一个正则表达式并将其传递给方法,还是根本不能传递正则表达式?String.contains String.contains用于字符串、句点。它不适用于正则表达式。它将检查指定的确切字符串是否出现在当前字符串中 请注意,String.contains不检查单词边界;

我想检查字符串是否按顺序包含单词“stores”、“store”和“product”,无论它们之间是什么

我尝试使用
someString.contains(存储%store%产品)
包含(“存储%store%产品”)

我是否需要显式声明一个正则表达式并将其传递给方法,还是根本不能传递正则表达式?

String.contains
String.contains
用于字符串、句点。它不适用于正则表达式。它将检查指定的确切字符串是否出现在当前字符串中

请注意,
String.contains
不检查单词边界;它只是检查子字符串

正则表达式解 Regex比String.contains更强大,因为您可以在关键字上强制使用单词边界(除其他外)。这意味着您可以搜索关键字作为单词,而不仅仅是子字符串

使用
String。将
与以下正则表达式匹配:

"(?s).*\\bstores\\b.*\\bstore\\b.*\\bproduct\\b.*"
原始正则表达式(删除字符串文本中完成的转义-这是打印上述字符串时得到的结果):

\b
检查单词边界,这样您就不会得到与
还原存储产品
匹配的内容。请注意,
stores 3store\u product
也被拒绝,因为digit和
\u
被视为单词的一部分,但我怀疑这种情况是否出现在自然文本中

因为两边都检查了单词边界,所以上面的正则表达式将搜索精确的单词。换句话说,
stores-product
将与上面的正则表达式不匹配,因为您正在搜索不带
s
的单词
store

通常匹配除以外的任何字符
(?s)
在开头使
毫无例外地匹配任何字符(感谢Tim Pietzcker指出这一点)。

matcher.find()
执行您需要的操作。例如:

Pattern.compile("stores.*store.*product").matcher(someString).find();

您可以简单地使用String类的
匹配方法

boolean result = someString.matches("stores.*store.*product.*");

如果要检查字符串是否包含子字符串或不使用正则表达式,可以使用find()

注意matches()和find()之间的区别,如果整个字符串与给定模式匹配,matches()将返回true。find()尝试查找与给定输入字符串中的模式匹配的子字符串。另外,通过使用find(),您不必在正则表达式模式的开头和结尾添加额外的匹配,如-(?s)。*

publicstaticvoidmain(字符串[]args){
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();
}
String test=“听到的东西,找到的东西,或拖到的东西”; System.out.println(“1.result:+包含(“-to-(\\w+)som”,test,null)); System.out.println(“2.result:+包含(“-to-(\\w+)som”,测试,5)); } 静态布尔包含(字符串模式、字符串文本、整数fromIndex){ if(fromIndex!=null&&fromIndex
1.结果:正确


2.result:true

Java11开始,可以使用返回
谓词的
模式#asMatchPredicate


如果字符串包含换行符,您可能需要将
(?s)
添加到正则表达式的开头。我正在这样的URL中检查它>>您能解释一下这里的第一个反斜杠吗
\\b
@vipin8169:在字符串中,您需要将
\
加倍以指定一个
\
,因此
\\b
将被解释为
\b
,如原始正则表达式中所示<代码>\b
匹配单词边界,如上所述。如果需要匹配字符串中的“.mydomain.”。那么它将如何更新正则表达式呢。我的用例是“www.abc.mydomain.in.io”是否包含.mydomain。或者我不喜欢这个。我发现matcher的正则表达式过于复杂。您需要从
*
开始,否则它将只匹配以
存储开始的字符串。尝试根据模式匹配整个区域。似乎@shmosel是对的,不是吗?它只是匹配,但不检查字符串是否在任何位置包含该模式。这不是OP想要的解决方案,我建议优化regexp。
fromIndex
被忽略,不是吗<代码>包含(“某物”,测试,5)=>true
    private static final validPattern =   "\\bstores\\b.*\\bstore\\b.*\\bproduct\\b"
    Pattern pattern = Pattern.compile(validPattern);
    Matcher matcher = pattern.matcher(inputString);
    System.out.print(matcher.find()); // should print true or false.
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();
}
String string = "stores%store%product";
String regex = "stores.*store.*product.*";
Predicate<String> matchesRegex = Pattern.compile(regex).asMatchPredicate();

boolean match = matchesRegex.test(string);                   // true
String string = "stores$store$product";
String regex = "stores.*store.*product.*";

Predicate<String> matchesRegex = Pattern.compile(regex).asMatchPredicate();
Predicate<String> hasLength = s -> s.length() > 20;

boolean match = hasLength.and(matchesRegex).test(string);    // false