简体   繁体   English

正则表达式匹配确切的子字符串java

[英]Regex to match exact substring java

I have a string as below 我有一个字符串如下

TestFilter('value') AND LoadTestFilter('value1') AND UserFilter(value2) OR TestFilter('value3')

I want to extract only TestFilter along with brackets from the above string. 我只想从上述字符串中提取TestFilter及其括号。 I want regex to match only these two substrings 我希望正则表达式仅匹配这两个子字符串

 TestFilter('value')
 TestFilter('value3')

I have tried below regex 我在正则表达式下面尝试过

.*TestFilter\\((.*?)\\).*

It is working but it is also matching LoadTestFilter in the string 它正在工作,但它也匹配字符串中的LoadTestFilter

How to match only TestFilter. 如何仅匹配TestFilter。

    String s = "TestFilter('value') AND LoadTestFilter('value1') AND UserFilter(value2) OR TestFilter('value3')";
    Matcher m = Pattern.compile("\\bTestFilter\\(.*?\\)").matcher(s);
    while (m.find()) {
        System.out.println(m.group(0));
    }

Explanation: 说明:

The key thing here is \\b , which matches a word boundary . 这里的关键是\\b ,它与单词boundary匹配。 \\b in the regex I used matches a word boundary at the very start of the match, and before TestFilter . 我使用的正则表达式中的\\b在匹配开始时和TestFilter之前匹配单词边界。 Translating to a less regex-like language, we don't want any letters before TestFilter . 翻译成TestFilter正则表达式的语言,我们不需要在TestFilter之前TestFilter任何字母。

According to your comment, you see to have tried .*\\bTestFlter\\(.*?\\) . 根据您的评论,您看到尝试过.*\\bTestFlter\\(.*?\\) This does not work because of the .* at the front. 由于前面带有。 .* ,因此无法使用。 You're basically matching a bunch of random characters, followed by a word boundary, "TestFilter" then a pair of brackets with random stuff in it. 基本上,您要匹配一堆随机字符,然后匹配单词边界“ TestFilter”,然后匹配其中带有随机内容的方括号。 This will match the whole string, since the last instance of TestFilter is preceded by a word boundary, then a bunch of random characters. 这将匹配整个字符串,因为TestFilter的最后一个实例之前是单词边界,然后是一堆随机字符。

Try this: 尝试这个:

TestFilter\\((.*?)\\).*

without first (.*) 没有第一个(。*)

\\b matches a word boundary in regex expressions. \\b匹配正则表达式中的单词边界。 So \\bTestFilter will match "TestFilter" but not "LoadTestFilter" , because there is no word boundary between "Load" and "Test" . 因此\\bTestFilter将匹配"TestFilter"但不匹配"TestFilter" "LoadTestFilter" ,因为在"Load""Test"之间没有单词边界。

Thus, you could use: 因此,您可以使用:

\\bTestFilter\\((.*?)\\)

or 要么

.*\\bTestFilter\\((.*?)\\).*

depending on how you are matching the input (eg, match() -vs- find() ) 取决于您如何匹配输入(例如match() vs- find()


Matcher#find example: Matcher#find示例:

String haystack = "TestFilter('value') AND LoadTestFilter('value1') AND UserFilter(value2) OR TestFilter('value3')";
Matcher needle = Pattern.compile("\\bTestFilter\\((.*?)\\)").matcher(haystack);

while(needle.find()) {
    System.out.format("[%s] found in [%s]%n", needle.group(1), needle.group());
}

Output: 输出:

['value'] found in [TestFilter('value')]
['value3'] found in [TestFilter('value3')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM