如何使用java将括号中的字符串转换为数组 [英] How to get strings in parentheses into array with java

查看:81
本文介绍了如何使用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将括号中的字符串转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆