Re: Splitting a String with a Regex




"Jussi Piitulainen" <jpiitula@xxxxxxxxxxxxxxxx> wrote in message news:qotbqufm2h7.fsf@xxxxxxxxxxxxxxxxxxxxxxxxx
Oliver Wong writes:

Danno wrote:
Try:

String s = "<?xml...><response
.../><?xml...><response.../><?xml...><response.../>";
String[] tokens = s.split("<\\?xml[.]*>");
for (String token : tokens) {
System.out.println(token);
}

Just a guess, I haven't tried it, so there maybe errors.

Probably won't work. XML is a context-free language, not a
regular language.

It might well work (maybe better with "<[?]xml.*?>" or so) for a
particular kind of input sequence where any <?xml...?> thing only
appears in the beginning of each individual part and nowhere else,
and the ... in any of them doesn't contain >.

Just looping to find each string "<?xml" would then also work.

Oops, I had thought that the regular expression Danno wrote was to get the content of the strings themselves, rather than the delimiters. So actually, Danno's code may probably work, as long as the "[.]*" part isn't greedy, along with the other qualifications you gave.

- Oliver

.



Relevant Pages