本文介绍了如何使用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 this?

推荐答案

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

这篇关于如何使用java将括号中的字符串转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 16:05
查看更多