Solved

Regeular expression to split a string

Posted on 2006-07-06
27
248 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:riaancornelius
  • 10
  • 7
  • 6
  • +1
27 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17049832
split( "\\[|\\]" );  
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 17049845
OK, why exactly? What is |
0
 
LVL 92

Expert Comment

by:objects
ID: 17049848
try:

String[] tokens = substring(s.indexOf("[")).split("\\]{0,1}\\[");
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 92

Expert Comment

by:objects
ID: 17049853
woops, should be:

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17049863
>> OK, why exactly? What is |

Or
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 17049867
>> String[] tokens = substring(s.indexOf("[")).split("\\]{0,1}\\[{0,1}");
Explain that please?
0
 
LVL 92

Expert Comment

by:objects
ID: 17049877
starting from first [
split on 0-1 ] followed by 0-1 [
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 17049880
I see CEHJ. Only problem is that it puts 4 blank elements as well.
0
 
LVL 35

Expert Comment

by:girionis
ID: 17049901
       String s = "KEYWORD [int][int][name&surname&int&cell_number][String]";
        String[] msg = s.substring(s.indexOf("[")).split( "\\[|\\]" );
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 17049913
objects, that just splits it at every single character and puts a space everywhere there was a [ or ][
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17049929
>>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
 
LVL 9

Author Comment

by:riaancornelius
ID: 17049930
girionis: Isn't that the same as CEHJ's first post?
0
 
LVL 35

Expert Comment

by:girionis
ID: 17049937
> girionis: Isn't that the same as CEHJ's first post?

Not exactly, I am getting rid of the "KEYWORD " as well.
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 17049939
>> 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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 17049942
it will too :)  try

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

Expert Comment

by:objects
ID: 17049949
> Was just wondering whether there was an easy solution (in the regex) to do that.

try my last post
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 17049957
>> 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
 
LVL 92

Expert Comment

by:objects
ID: 17049968
substring I'd reckon
0
 
LVL 35

Expert Comment

by:girionis
ID: 17049972
> >> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17049982
>> 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
 
LVL 9

Author Comment

by:riaancornelius
ID: 17049984
good plan objects.

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

This works perfectly.
0
 
LVL 92

Expert Comment

by:objects
ID: 17049996
excellent :)
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 17050003
>> 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
 
LVL 35

Expert Comment

by:girionis
ID: 17050019
That would work too. As you see there are several solutions to a problem :)
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 17050039
I was just making it too complex...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17050221
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17050237
This is more efficient:

String[] tokens = s.replaceFirst("KEYWORD\\s*\\[", "").split("\\]\\[|\\]");
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question