【问题标题】:How to get strings in parentheses into array with java如何使用java将括号中的字符串放入数组中
【发布时间】:2011-09-18 01:18:58
【问题描述】:

假设我有这样的字符串:

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

我想将括号中的字符串提取到字符串数组中

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

commons 包中有什么东西或其他技巧可以做到这一点吗?

【问题讨论】:

  • 尝试“这行得通吗”这个问题很容易......为此很难阻止 -1。

标签: java string extract apache-commons


【解决方案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));

【讨论】:

    猜你喜欢
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多