且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在字符串中输入括号内的值

更新时间:2023-02-18 09:58:37

编译并打印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));    
     }
  }
}

正则表达式意味着:


  • \\(:字符

  • :start match group

  • [:其中一个字符

  • ^ :不是以下字符

  • :使用之前的 ^ ,这意味着除了之外的每个字符

  • + :来自 []的其中一项内容设置

  • :停止匹配组

  • \\):文字闭幕式

  • \\(: 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