split string (with escaped char) using regexp

Hi,

I would split the following string using ; as delimitator but pay attention to \;

String n = N:name\; surname;middlename

I would have as result

values[0] = name\; surname

values[1] = middlename

In fact, if into string there is an escaped ; this char must not consider a delimitator.

If I write

Pattern p = Pattern.compile("[^(\\\\)];");

String values[] = p.split(n);

I've a wrong result

values[0] = name\; surnam<-- there is not the last char

values[1] = middlename

How can I write the regexp into split?

Thanks,

Luigia

[631 byte] By [luigiaa] at [2007-11-27 7:01:19]
# 1

From the java.util.regex.Pattern class API doc:

(?<!X) X, via zero-width negative lookbehind

Here's an example:

public class Luigia{

public static void main(String[] args){

String text = "N:name\\; surname;middlename";

String regex = "(?<!\\\\);";

text = text.substring(2); // cut "N:" off

String[] sa = text.split(regex);

for (String s : sa){

s = s.trim();

System.out.println(s);

}

}

}

Message was edited by:

hiwa>

hiwaa at 2007-7-12 18:52:07 > top of Java-index,Java Essentials,Java Programming...
# 2
It works!Thank youLuigia
luigiaa at 2007-7-12 18:52:07 > top of Java-index,Java Essentials,Java Programming...