如何在String.contains()方法中使用regex ?

时间:2022-12-30 19:03:41

I want to check if a String contains the words "stores", "store" and "product" in that order. No matter what is in between them.

我想检查一个字符串是否包含“存储”、“存储”和“产品”。不管他们之间有什么。

I tried using someString.contains(stores%store%product); and also .contains("stores%store%product");

我试着使用someString.contains(商店存储产品% %);而且.contains(“商店% %存储产品”);

Do i need to explicitly declare a regex and pass it on the method or i can't pass a regex at all?

我是否需要显式地声明regex并将其传递到方法上,否则我无法传递正则表达式?

5 个解决方案

#1


91  

String.contains

String.contains works with String, period. It doesn't work with regex. It will check whether the exact String specified appear in the current String or not.

字符串。包含与字符串的工作,周期。它不能与regex一起工作。它将检查指定的确切字符串是否出现在当前字符串中。

Note that String.contains does not check for word boundary; it simply checks for substring.

注意,字符串。包含不检查单词边界;它只是检查子字符串。

Regex solution

Regex is more powerful than String.contains, since you can enforce word boundary on the keywords (among other things). This means you can search for the keywords as words, rather than just substrings.

Regex比String更强大。包含,因为您可以在关键字(包括其他内容)上执行单词边界。这意味着您可以搜索关键字作为单词,而不仅仅是子字符串。

Use String.matches with the following regex:

使用字符串。与以下regex匹配:

"(?s).*\\bstores\\b.*\\bstore\\b.*\\bproduct\\b.*"

The RAW regex (remove the escaping done in string literal - this is what you get when you print out the string above):

原始的regex(删除字符串字面上的转义——这是您打印上面的字符串时得到的):

(?s).*\bstores\b.*\bstore\b.*\bproduct\b.*

The \b checks for word boundary, so that you don't get a match for restores store products. Note that stores 3store_product is also rejected, since digit and _ are considered part of a word, but I doubt this case appear in natural text.

\b检查单词边界,这样你就不会得到商店产品的匹配。注意,存储3store_product也被拒绝,因为digit和_被认为是一个单词的一部分,但是我怀疑这种情况是否出现在自然文本中。

Since word boundary is checked for both sides, the regex above will search for exact words. In other words, stores stores product will not match the regex above, since you are searching for the word store without s.

由于两边都要检查单词边界,所以上面的regex将搜索确切的单词。换句话说,store product将不匹配上面的regex,因为您正在搜索没有s的word store。

. normally match any character except a number of new line characters. (?s) at the beginning makes . matches any character without exception (thanks to Tim Pietzcker for pointing this out).

。通常匹配任何字符,除了一些新的行字符。(s)开始。毫无例外地匹配任何字符(感谢Tim Pietzcker指出这一点)。

#2


71  

matcher.find() does what you needed. Example:

find()执行所需的操作。例子:

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

#3


14  

You can simply use matches method of String class.

您可以简单地使用String类的matches方法。

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

#4


0  

If you want to check if a string contains substring or not using regex, the closest you can do is by using find() -

如果您想检查一个字符串是否包含子字符串,或者不使用正则表达式,那么您可以使用find() -。

    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.

Note the difference between matches() and find(), matches() return true if the whole string matches the given pattern. find() tries to find a substring that matches the pattern in a given input string. Also by using find() you don't have to add extra matching like - (?s).* at the beginning and .* at the end of your regex pattern.

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

#5


0  

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.result: true

1。结果:真正的

2.result: true

2。结果:真正的

#1


91  

String.contains

String.contains works with String, period. It doesn't work with regex. It will check whether the exact String specified appear in the current String or not.

字符串。包含与字符串的工作,周期。它不能与regex一起工作。它将检查指定的确切字符串是否出现在当前字符串中。

Note that String.contains does not check for word boundary; it simply checks for substring.

注意,字符串。包含不检查单词边界;它只是检查子字符串。

Regex solution

Regex is more powerful than String.contains, since you can enforce word boundary on the keywords (among other things). This means you can search for the keywords as words, rather than just substrings.

Regex比String更强大。包含,因为您可以在关键字(包括其他内容)上执行单词边界。这意味着您可以搜索关键字作为单词,而不仅仅是子字符串。

Use String.matches with the following regex:

使用字符串。与以下regex匹配:

"(?s).*\\bstores\\b.*\\bstore\\b.*\\bproduct\\b.*"

The RAW regex (remove the escaping done in string literal - this is what you get when you print out the string above):

原始的regex(删除字符串字面上的转义——这是您打印上面的字符串时得到的):

(?s).*\bstores\b.*\bstore\b.*\bproduct\b.*

The \b checks for word boundary, so that you don't get a match for restores store products. Note that stores 3store_product is also rejected, since digit and _ are considered part of a word, but I doubt this case appear in natural text.

\b检查单词边界,这样你就不会得到商店产品的匹配。注意,存储3store_product也被拒绝,因为digit和_被认为是一个单词的一部分,但是我怀疑这种情况是否出现在自然文本中。

Since word boundary is checked for both sides, the regex above will search for exact words. In other words, stores stores product will not match the regex above, since you are searching for the word store without s.

由于两边都要检查单词边界,所以上面的regex将搜索确切的单词。换句话说,store product将不匹配上面的regex,因为您正在搜索没有s的word store。

. normally match any character except a number of new line characters. (?s) at the beginning makes . matches any character without exception (thanks to Tim Pietzcker for pointing this out).

。通常匹配任何字符,除了一些新的行字符。(s)开始。毫无例外地匹配任何字符(感谢Tim Pietzcker指出这一点)。

#2


71  

matcher.find() does what you needed. Example:

find()执行所需的操作。例子:

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

#3


14  

You can simply use matches method of String class.

您可以简单地使用String类的matches方法。

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

#4


0  

If you want to check if a string contains substring or not using regex, the closest you can do is by using find() -

如果您想检查一个字符串是否包含子字符串,或者不使用正则表达式,那么您可以使用find() -。

    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.

Note the difference between matches() and find(), matches() return true if the whole string matches the given pattern. find() tries to find a substring that matches the pattern in a given input string. Also by using find() you don't have to add extra matching like - (?s).* at the beginning and .* at the end of your regex pattern.

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

#5


0  

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.result: true

1。结果:真正的

2.result: true

2。结果:真正的