Regeular expression to split a string

I have a string in this format:
KEYWORD [int][int][name&surname&int&cell_number][String]

I want to strip out the keyword (leaving me with: '[int][int][name&surname&int&cell_number][String]' ) and then split the rest up into:
int
int
name&surname&int&cell_number
String

I thought something like:
String[] msg = message.replaceFirst("CT_FEEDBACK ", "").split( "\\[.*?\\]" );  
would work, but it doesn't...

Any ideas?
LVL 9
riaancorneliusAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
split( "\\[|\\]" );  
0
riaancorneliusAuthor Commented:
OK, why exactly? What is |
0
Mick BarryJava DeveloperCommented:
try:

String[] tokens = substring(s.indexOf("[")).split("\\]{0,1}\\[");
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Mick BarryJava DeveloperCommented:
woops, should be:

String[] tokens = substring(s.indexOf("[")).split("\\]{0,1}\\[{0,1}");

0
CEHJCommented:
>> OK, why exactly? What is |

Or
0
riaancorneliusAuthor Commented:
>> String[] tokens = substring(s.indexOf("[")).split("\\]{0,1}\\[{0,1}");
Explain that please?
0
Mick BarryJava DeveloperCommented:
starting from first [
split on 0-1 ] followed by 0-1 [
0
riaancorneliusAuthor Commented:
I see CEHJ. Only problem is that it puts 4 blank elements as well.
0
girionisCommented:
       String s = "KEYWORD [int][int][name&surname&int&cell_number][String]";
        String[] msg = s.substring(s.indexOf("[")).split( "\\[|\\]" );
0
riaancorneliusAuthor Commented:
objects, that just splits it at every single character and puts a space everywhere there was a [ or ][
0
CEHJCommented:
>>I see CEHJ. Only problem is that it puts 4 blank elements as well.

Yes. That's not easy to avoid. Just ignore elements in the array that are blank
0
riaancorneliusAuthor Commented:
girionis: Isn't that the same as CEHJ's first post?
0
girionisCommented:
> girionis: Isn't that the same as CEHJ's first post?

Not exactly, I am getting rid of the "KEYWORD " as well.
0
riaancorneliusAuthor Commented:
>> Yes. That's not easy to avoid. Just ignore elements in the array that are blank
Was just wondering whether there was an easy solution (in the regex) to do that.

Thanks CEHJ
0
Mick BarryJava DeveloperCommented:
it will too :)  try

        String[] msg = s.substring(s.indexOf("[")+1, s.length()-1).split( "\\]\\[" );
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mick BarryJava DeveloperCommented:
> Was just wondering whether there was an easy solution (in the regex) to do that.

try my last post
0
riaancorneliusAuthor Commented:
>> Not exactly, I am getting rid of the "KEYWORD " as well.
My original solution does get rid of it as well.

As a matter of interest, Anybody know which is more efficient? substring() or replaceFirst().
0
Mick BarryJava DeveloperCommented:
substring I'd reckon
0
girionisCommented:
> >> Not exactly, I am getting rid of the "KEYWORD " as well.
> My original solution does get rid of it as well.

Yes you are right, but my comment will replace *any* keyword, not just the "CT_FEEDBACK"
0
CEHJCommented:
>> Was just wondering whether there was an easy solution (in the regex) to do that.

Can't think of one without substringing at the moment
0
riaancorneliusAuthor Commented:
good plan objects.

 String[] msg = s.substring(s.indexOf("[")+1, s.length()-1).split( "\\]\\[" );

This works perfectly.
0
Mick BarryJava DeveloperCommented:
excellent :)
0
riaancorneliusAuthor Commented:
>> Yes you are right, but my comment will replace *any* keyword, not just the "CT_FEEDBACK"
This is true. I didn't pick that up, because in this class, keyword will always be "CT_FEEDBACK".

Interesting thought here though. If it's such a simple solution, it would actually be more efficient to use:
StringTokenizer st = new StringTokenizer( message.substring(message.indexOf("[")+1, message.length()-1), "][" );

Since stringTokenizer is a lot faster than String.split().
0
girionisCommented:
That would work too. As you see there are several solutions to a problem :)
0
riaancorneliusAuthor Commented:
I was just making it too complex...
0
CEHJCommented:
>>Was just wondering whether there was an easy solution (in the regex) to do that.

You can do it like this actually:

String[] tokens = s.replaceFirst("KEYWORD\\s*\\[", "").split("\\[|\\]\\[|\\]");
0
CEHJCommented:
This is more efficient:

String[] tokens = s.replaceFirst("KEYWORD\\s*\\[", "").split("\\]\\[|\\]");
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.