[英]Java Pattern matching group value containing parantheses
I am trying to group a string expression into three parts using Regex.我正在尝试使用正则表达式将字符串表达式分为三个部分。 It works for most cases and getting stuck for one case.
它适用于大多数情况,但在一种情况下会卡住。
Regex正则表达式
([\\w.]+?)(:|<|>|<=|>=|%|-|\\(\\))([\\w\\s,.:-]+?)\\|
Code:代码:
private SearchCriteria validateFilterPattern(String filter) {
final Pattern pattern = Pattern.compile("([\\w.]+?)(:|<|>|<=|>=|%|-|\\(\\))([\\w\\s,.:-]+?)\\|");
Matcher m = pattern.matcher(filter + "|");
if (m.find()) {
return SearchCriteria.builder().key(m.group(1)).operator(m.group(2)).value(m.group(3)).build();
} else {
throw new RuntimeException(ErrorMessage.FILTER_FORMAT_INVALID, filter);
}
}
Input is having pattern key:value.输入具有模式键:值。 The above code is using the regex and grouping the input expression into three parts: key,operator and value.
上面的代码使用正则表达式并将输入表达式分组为三个部分:键、运算符和值。 Operators can be: , >, <, >=, <=, *, ~, (), % Keys can be any word, value can contain word, letters, dots, colon,some special characters and brackets.
运算符可以是:, >, <, >=, <=, *, ~, (), % 键可以是任何单词,值可以包含单词、字母、点、冒号、一些特殊字符和括号。
I am able to match following and group it into three parts.我能够匹配以下并将其分为三个部分。
regulatory:Section 740.17 Mass Market
regulatory:Section 740.17
The above two inputs segregate into groups like上述两个输入分为如下组
key {regulatory} , operator {:} , value {Section 740.17 Mass Market}
key {regulatory} , operator {:} , value {Section 740.17}
But not able to group it for inputs where value contains brackets.但无法将其分组为值包含括号的输入。
regulatory:Section 740.17(b)(1) Mass Market
regulatory()Section 740.17(b)(1) Mass Market
The above should segregate into groups like以上内容应分为以下组
key {regulatory} , operator {:} , value {Section 740.17(b)(1) Mass Market}
key {regulatory} , operator {()} , value {Section 740.17(b)(1) Mass Market}
Able to solve it by using below regex能够通过使用下面的正则表达式来解决它
"([\\w.]+?)(:|<|>|<=|>=|%|-|\\(\\))([\\w\\s,.:\\(\\)-]+?)\\|"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.