0

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]
5
  • Can you give a sample output for this code? Commented Mar 23, 2013 at 4:43
  • 1
    Oh, I see. This isn't really regex, you can use a simple split. Commented Mar 23, 2013 at 4:44
  • the splitter should work, but i want to use the Pattern, like RegEx.match Commented Mar 23, 2013 at 4:53
  • The question is unclear. "[1]" is a regex. Or do you mean that [1] stands in for the first list item? Or is 1 the list items, and the brackets and the dashes are literals? Commented Mar 23, 2013 at 4:55
  • Hi Joshua, the last part is the result i should get from the Pattern. and all the brackets and dashes are literals Commented Mar 23, 2013 at 5:01

3 Answers 3

3
,(?=\[)

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]
Sign up to request clarification or add additional context in comments.

9 Comments

i have a pattern, but it will split the [6,7,8] into three parts, it is ""[^""\r\n]*""|'[^'\r\n]*'|[^,\r\n]*
My pattern specifically preserves [6,7,8]
could you be able to elaborate your pattern please. i tried your pattern, but it does not work
Perhaps it should be -(?=\[)? That is, a hyphen that is followed by a bracket?
Sorry, i should mention i have to use the Pattern, not Split. there should be a Pattern, but cannot figure it out yet. anyway thanks for your code example :)
|
0

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]

1 Comment

thanks for that. it is exactly what i want. you are champion : )
0

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 "@"

1 Comment

And if the string has any @ symbols to begin with, you're out of luck. The other answer offers a far better solution that doesn't encounter this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.