如何获取字符串中的括号值? [英] How can I get inside parentheses value in a string?

查看:106
本文介绍了如何获取字符串中的括号值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取字符串中的括号值?

How can I get inside parentheses value in a string?

String str= "United Arab Emirates Dirham (AED)";

我只需要AED文本.

推荐答案

编译并打印"AED".甚至适用于多个括号:

Compiles and prints "AED". Even works for multiple parenthesis:

import java.util.regex.*;

public class Main
{
  public static void main (String[] args)
  {
     String example = "United Arab Emirates Dirham (AED)";
     Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(example);
     while(m.find()) {
       System.out.println(m.group(1));    
     }
  }
}

正则表达式的意思是:

  • \\(:字符(
  • (:开始比赛组
  • [:这些字符之一
  • ^ :不是以下字符
  • ):使用前一个 ^ ,表示除)之外的所有字符"
  • + : []
  • 中的其他内容之一
  • ):停止比赛组
  • \\):文字封闭式括号
  • \\(: character (
  • (: start match group
  • [: one of these characters
  • ^: not the following character
  • ): with the previous ^, this means "every character except )"
  • +: one of more of the stuff from the [] set
  • ): stop match group
  • \\): literal closing paranthesis

这篇关于如何获取字符串中的括号值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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