Solved

Regeular expression to split a string

Posted on 2006-07-06
27
231 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now