Home > Back-end >  How to divide a string with a regex through characters that are outside square brackets?
How to divide a string with a regex through characters that are outside square brackets?

Time:03-17

I have this string:

PARTNER6;PARTNER7[PORTAL4;PORTAL5];PARTNER1[PARTNER1WEB]    -> ∞

I want to divide it like this:

PARTNER6

PARTNER7[PORTAL4;PORTAL5]

PARTNER1[PARTNER1WEB]

I tried to use this expression, but it divides everything including what is in parentheses

[\s,;] 

I can't figure out how to divide only what is outside the brackets

CodePudding user response:

I would use a regex find all approach here:

String input = "PARTNER6;PARTNER7[PORTAL4;PORTAL5];PARTNER1[PARTNER1WEB]";
String pattern = "(\\w (?:\\[.*?\\])?);?";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
while (m.find()) {
    System.out.println(m.group(1));
}

This prints:

PARTNER6
PARTNER7[PORTAL4;PORTAL5]
PARTNER1[PARTNER1WEB]

The regex pattern used above matches a word with \w , followed by an optional term in square brackets, followed by optional semicolon.

CodePudding user response:

You may use this regex to get all of your matches:

\w (?:\[[^]]*\]\w*)*\w*

RegEx Demo

RegEx Details:

  • \w : Match 1 word characters
  • (?:\[[^]]*\]\w*)*: Match [...] string followed by 0 or more word characters. Repeat this group 0 or more times
  • \w*: Match 0 or more word characters

Code:

jshell> String regex = "\\w (?:\\[[^]]*\\]\\w*)*\\w*";
regex ==> "\\w (?:\\[[^]]*\\]\\w*)*\\w*"

jshell> String string = "PARTNER6;PARTNER7[PORTAL4;PORTAL5];PARTNER1[PARTNER1WEB]    -> ∞";
string ==> "PARTNER6;PARTNER7[PORTAL4;PORTAL5];PARTNER1[PARTNER1WEB]    -> ∞"

jshell>  Pattern.compile(regex).matcher(string).results().map(MatchResult::group).collect(Collectors.toList());
$3 ==> [PARTNER6, PARTNER7[PORTAL4;PORTAL5], PARTNER1[PARTNER1WEB]]
  • Related