Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

How to split a string with regular expression

Thread view: 
rhaavik - 16 Nov 2005 16:15 GMT
Hi.
I need to split a string with a regular expression.
Example
String = "this is; a test";rune haavik;12345;
And I want the output to be:
"this is; a test"
rune haavik
12345

If I use this code:
 private void test1()
 {
     String str = "\"this is; a test\";rune haavik;12345;";
     int i=0;
     String[] tmp = str.split(";");
     while(i<tmp.length)
     {
       System.out.println(tmp[i]);
       i++;
     }
 }
Then it splits also in the "" text.

Regards
Rune haavik
klynn47@comcast.net - 16 Nov 2005 16:32 GMT
Could you elaborate?

Then it splits also in the "" text.
Oliver Wong - 16 Nov 2005 16:41 GMT
> Hi.
> I need to split a string with a regular expression.
[quoted text clipped - 18 lines]
>  }
> Then it splits also in the "" text.

   For this particular task, I recommend against using regular expressions.
I think it'll be much simpler if you do a character by character scan of the
string, looking for ';', and toggling a flag every time you see '"'.

   - Oliver
Roedy Green - 16 Nov 2005 20:43 GMT
>String = "this is; a test";rune haavik;12345;
>And I want the ou

you don't want ; inside " " to be treated as ;

What you are doing is reading a CSV file. It will be much easier with
a tool specialised for the job rather than attempting to write a tool
with a generic regex.

see http://mindprod.com/jgloss/csv.html

CSVReader can be configured to use ; instead of ,.  I take it you are
In Sweden where MS products use ; instead of , in CSV files.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

rhaavik - 17 Nov 2005 06:45 GMT
Hi.
I know I'm reading a csv file. And it is a dump from SAP.
Because of other things I need to do with the contents in the file, I'm
looking for a regex to do the job.
Can anyone help with that?

Thanx
Rune
Roedy Green - 17 Nov 2005 07:55 GMT
>I know I'm reading a csv file. And it is a dump from SAP.
>Because of other things I need to do with the contents in the file, I'm
>looking for a regex to do the job.
>Can anyone help with that?

Let's say someone kludges the ; for you. You will in a day or two come
back and want the embedded quotes handled properly.  You are on the
thin fringes of what you can do with regex.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Hendrik Maryns - 17 Nov 2005 10:19 GMT
rhaavik schreef:
> Hi.
> I know I'm reading a csv file. And it is a dump from SAP.
> Because of other things I need to do with the contents in the file, I'm
> looking for a regex to do the job.
> Can anyone help with that?

Well, if you really need to, you might get more help on
comp.lang.perl.misc, but there they will also forward you to some
library which does balanced parens and stuff.

H.

Signature

Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.