Frage

Here is a String, I want to split the string such that the symbols and "words" are separate. For example:

String s = "/xyz/abc[bcd(text(),\"string\")]";

I want to have a String array like

String[] result = {"/","xyz","/","abc","[","bcd","(","text","(",")",",","\"","string","\"",")","]"}

How can I do this by using regular expression?

War es hilfreich?

Lösung

This should work:

String str = "/xyz/abc[bcd(text(),\"string\")]";

String[] arr = str.split("(?<=\\G(?>\\w+|\\W))\\s*");

This gives:

["/", "xyz", "/", "abc", "[", "bcd", "(", "text", "(", ")", ",", """, "string", """, ")", "]"]

Explanation:

Andere Tipps

User anubhava has answered your question, but in the future http://regexpal.com/ is very helpful when regexing. I use it all the time as a tool, though there are many options.

Helps with trial and error :)

Good luck, happy coding

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top