How to get strings in parentheses into array with java

不羁岁月 提交于 2019-12-23 03:55:09

问题


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 this?


回答1:


    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));


来源:https://stackoverflow.com/questions/6400849/how-to-get-strings-in-parentheses-into-array-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!