Alex
Alex

Reputation: 4934

RegEx help tokenizing a string

Could anyone tell me how I can take this: (King's_Cross_St_Pancras, Farringdon, Hammersmith_&_City_Line)

And split it into:

array[0]: King's_Cross_St_Pancras
array[1]: Farringdon
array[2]: Hammersmith_&_City_Line

The regex should also account for < > non-word characters too. I have tried to make the regex using: "\b(<>&')?" .. this is however completely useless.

Please help

Upvotes: 0

Views: 96

Answers (2)

magnattic
magnattic

Reputation: 12998

You can do this easier and quicker without an RegEx:

String str = "(King's_Cross_St_Pancras, Farringdon, Hammersmith_&_City_Line)";
String result[] = str.substring(1, str.length()-1).split(",");

Upvotes: 1

moinudin
moinudin

Reputation: 138317

There's no need to use a regular expression here. Strip the () using String.substring(), then split the string into tokens using String.split(). Although technically this is also a regex solution, as String.split() takes a regex. :)

String s = "(King's_Cross_St_Pancras, Farringdon, Hammersmith_&_City_Line)";
s = s.substring(1, s.length()-1);
String tokens[] = s.split(", ");

Result:

King's_Cross_St_Pancras
Farringdon
Hammersmith_&_City_Line

http://ideone.com/zSDN5

Upvotes: 5

Related Questions