[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regeular expression to split a string

Posted on 2006-07-06
27
Medium Priority
?
279 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 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