我的模式如下
API:ADD|TYPE:ABC...MATCH:TRUE
[LOTS OF OTHER LOG LINES]
API:ADD|TYPE:ABC...MATCH:TRUE
[LOTS OF OTHER LOG LINES]
API:ADD|TYPE:DEF...MATCH:TRUE
我尝试了以下正则表达式:
(API:.*MATCH:(TRUE|FALSE))
while (matcher.find()) {
System.out.println(i + " occurence");
i++;
matches.add(matcher.group());
}
它从第一个“API”匹配到最后一个“TRUE”,因此只返回一个子字符串!我想要三个子串(在这种情况下)从“API”开始直到“TRUE”或“FALSE”。
感谢你在这方面的帮助。谢谢。
修改
-------------------------------------------------------
20:31:57 CALL add 35
-------------------------------------------------------
20:31:57 REASON API:ADD|TYPE:ABC|ErrorType:VALIDATION|Error Message:User already has|MATCH:FALSE
答案 0 :(得分:1)
使用非贪婪量词.*?
,正则表达式将尽可能少地开始匹配。
(API:.*?MATCH:(TRUE|FALSE))
答案 1 :(得分:1)
This is because you use .*
which is a greedy quantifier.
You should try to use .*?
which is reluctant -- that means that it matches the smallest possible substring.
For more info about greedy vs. reluctant see the excellent answers here: Greedy vs. Reluctant vs. Possessive Quantifiers