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