要保持简单,以下示例:
iphone Foo.bar.StartTimestamp:[2012-11-12 TO 2016-02-15] and apple Bar.Foo.BarTimestamp:[2012-11-12 TO 2016-02-15] apple
从上面的文本中,我想使用Regex过滤Foo.bar.StartTimestamp:[2012-11-12 TO 2016-02-15]
和Bar.Foo.BarTimestamp:[2012-11-12 TO 2016-02-15]
。可以有任何组合而不是Bar.Foo.BarTimestamp:[2012-11-12 TO 2016-02-15]
,但它的格式相同。
我尝试了此(?<!\S)[][^[]]*
正则表达式,但它仅滤波了被方括号包围的文本。
我应该如何构架正则表达式以获得所需的结果?
以下是Regex101.com的链接:https://www.regex101.com/r/qlp4jb/1
您可以使用此正则表达式,而无需任何外观:
(?:w+.)+w+:[[^]]+]
REGEX DEMO
Java代码
final String regex = "(?:\w+\.)+\w+:\[[^]]+\]";
final String string = "iphone Foo.bar.StartTimestamp:[2012-11-12 TO 2016-02-15] and apple Bar.Foo.BarTimestamp:[2012-11-12 TO 2016-02-15] apple";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Matched: " + matcher.group(0));
}
尝试以下等级:
w+(.w+)*:[[^]]*]
REGEX TESTER。