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
Solved

Regeular expression to split a string

Posted on 2006-07-06
27
254 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 34
SHA2 certs for IIS AND Java? 2 113
Java: The Public Class Main 4 30
learn programming 8 34
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

840 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