开发者

How to get strings in parentheses into array with java

Lets say I have string like this:

String q = "foo (one) bla (two) zoo key hola (tree) (four) five"

I want to extract the strings in parentheses into string array

so this would be true:
stringArray[3].equals("four") 

Is there something in commons package or other trick to do thi开发者_开发知识库s?


    Pattern p = Pattern.compile("\\((.*?)\\)");

    Matcher m = p.matcher("abc (one) abc (two) abc");

    List<String> result = new ArrayList<String>();

    while(m.find())
        result.add(m.group(1));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜