0

I have a string like this:

String unparsed = "[thing.1][thin2g]"

I want to turn it into

"thing.1"
"thin2g"

Been trying for a while with regex expressions but nothing. Any thoughts? Thanks!

EDIT:

Tried:

String unparsed = "[thing.1][thin2g]"
String substring = unparsed.substring(1,unparsed.length - 1)
substring.replace("][","`")
String[] split = substring.split('`')
for(int i=0;i<split.length;i++)
{
   System.out.println(split[i])
}

But this seems kinda heavy, was looking for something more elegant

1
  • 5
    Show us what you tried. Use the Pattern class. Commented Aug 14, 2013 at 1:42

2 Answers 2

1
String unparsed = "[thing.1][thin2g]";
Pattern pattern = Pattern.compile("\\[(.*?)\\]");
Matcher matcher = pattern.matcher(unparsed);
while(matcher.find()){
    System.out.println(matcher.group(1));
}

My regex is not good. But it does parse the string into what you want.

Sign up to request clarification or add additional context in comments.

Comments

0
\[\s*\w.*?\]

I never use regular expressions before but this one should work

Comments

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.