Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regeular expression to split a string

Posted on 2006-07-06
27
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

610 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