Filter words that have periods and ends with square brackets

To keep it simple, let's look at the following example:

iphone Foo.bar.StartTimestamp:[2012-11-12 TO 2016-02-15] and apple Bar.Foo.BarTimestamp:[2012-11-12 TO 2016-02-15] apple

from the text above, I would like to filter Foo.bar.StartTimestamp:[2012-11-12 TO 2016-02-15]and Bar.Foo.BarTimestamp:[2012-11-12 TO 2016-02-15]using regex. there may be any combinations instead Bar.Foo.BarTimestamp:[2012-11-12 TO 2016-02-15], but it will be in the same format.

I tried this regex (?<!\\S)[][^[]]*, but its only filtering text surrounded by square brackets.

How do I create a regular expression to get the desired result?

here is a link to regex101.com: https://www.regex101.com/r/QLP4jB/1

+4
source share
2 answers

Try this regex:

\w+(\.\w+)*:\[[^]]*\]

Regular Expression Tester

+2

- :

(?:\w+\.)+\w+:\[[^]]+\]

- RegEx

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));
}
+2

Source: https://habr.com/ru/post/1664816/


All Articles