How to split a string with regular expression

R

rhaavik

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++;
}
}
Then it splits also in the "" text.

Regards
Rune haavik
 
O

Oliver Wong

rhaavik said:
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++;
}
}
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
 
R

Roedy Green

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.
 
R

rhaavik

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
 
R

Roedy Green

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.
 
H

Hendrik Maryns

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.

--
Hendrik Maryns

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top