user1629480
user1629480

Reputation: 13

Using RegEx split the string

I have a string like '[1]-[2]-[3],[4]-[5],[6,7,8],[9]' or '[Computers]-[Apple]-[Laptop],[Cables]-[Cables,Connectors],[Adapters]', I'd like the Pattern to get the list result, but don't know how to figure out the pattern. Basically the comma is the split, but [6,7,8] itself contains the comma as well.

the string: [1]-[2]-[3],[4]-[5],[6,7,8],[9]
the result:
[1]-[2]-[3]
[4]-[5]
[6,7,8]
[9]

or

the string: [Computers]-[Apple]-[Laptop],[Cables]-[Cables,Connectors],[Adapters]
the result:
[Computers]-[Apple]-[Laptop]
[Cables]-[Cables,Connectors]
[Adapters]

Upvotes: 0

Views: 145

Answers (3)

FThompson
FThompson

Reputation: 28687

Although I believe the best approach here is to use split (as presented by @j__m's answer), here's an approach that uses matching rather than splitting.

Regex:

(\[.*?\](?!-))

Example usage:

String input = "[Computers]-[Apple]-[Laptop],[Cables]-[Cables,Connectors],[Adapters]";
Pattern p = Pattern.compile("(\\[.*?\\](?!-))");
Matcher m = p.matcher(input);
while (m.find()) {
    System.out.println(m.group(1));
}

Resulting output:

[Computers]-[Apple]-[Laptop]
[Cables]-[Cables,Connectors]
[Adapters]

Upvotes: 0

j__m
j__m

Reputation: 9625

,(?=\[)

This pattern splits on any comma that is followed by a bracket, but keeps the bracket within the result text.

The (?=*stuff*) is known as a "lookahead assertion". It acts as a condition for the match but is not itself part of the match.

In C# code:

String inputstring = "[Computers]-[Apple]-[Laptop],[Cables]-[Cables,Connectors],[Adapters]";
foreach(String s in Regex.Split(inputstring, @",(?=\[)"))
    System.Console.Out.WriteLine(s);

In Java code:

String inputstring = "[Computers]-[Apple]-[Laptop],[Cables]-[Cables,Connectors],[Adapters]";
Pattern p = Pattern.compile(",(?=\\[)"));
for(String s : p.split(inputstring))
    System.out.println(s);

Either produces:

[Computers]-[Apple]-[Laptop]
[Cables]-[Cables,Connectors]
[Adapters]

Upvotes: 3

composerMike
composerMike

Reputation: 1096

An answer that doesn't use regular expressions (if that's worth something in ease of understanding what's going on) is:

  1. substitute "]@[" for "],["
  2. split on "@"

Upvotes: 0

Related Questions