java - How to extract the desired strings from a bracketed string -


sorry bad title. feel free make better title.

i have string follows

[munich, germany],[jingle pot rd, nanaimo, bc, canada],... 

now want convert list linkedlist locations where

 0 index -> munich, germany       1 index -> jingle pot rd, nanaimo, bc, canada 

and on..

how can efficiently in java. thought of using string.split() efficient way this.

try matching capturing group on "anything between brackets" (e.g. \[(.*?)\]) , adding group list:

public static list<string> getstringsinbrackets(string s) {   pattern p = pattern.compile("\\[(.*?)\\]");   matcher m = p.matcher(s);   list<string> list = new arraylist<string>();   while (m.find()) { list.add(m.group(1)); }   return list; } 

for example:

public static void main(string args[]) {   string s = "[munich, germany],[jingle pot rd, nanaimo, bc, canada]";   list<string> ss = getstringsinbrackets(s);   ss.get(0); // => "munich, germany"   ss.get(1); // => "jingle pot rd, nanaimo, bc, canada" } 

if performance becomes concern can pre-compile pattern.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -