Java 提取括号之间文本的模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24256478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:08:25  来源:igfitidea点击:

Pattern to extract text between parenthesis

javapattern-matching

提问by Rajeev Sahu

How to extract string from "("and ")"using pattern matching or anything. For example if the text is `

如何从"("")"使用模式匹配或任何东西中提取字符串。例如,如果文本是`

"Hello (Java)"

“你好(爪哇)”

Then how to get only "Java".

那么怎么获得呢"Java"

Thanks.

谢谢。

采纳答案by Rahul Tripathi

Try this:

尝试这个:

 String x= "Hello (Java)";
 Matcher m = Pattern.compile("\((.*?)\)").matcher(x);
while(m.find()) {
    System.out.println(m.group(1));
}

or

或者

String str = "Hello (Java)";
String answer = str.substring(str.indexOf("(")+1,str.indexOf(")"));

回答by CoderCroc

List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("\((.*?)\)");
Matcher regexMatcher = regex.matcher("Hello This is (Java) Not (.NET)");

while (regexMatcher.find()) {//Finds Matching Pattern in String
   matchList.add(regexMatcher.group(1));//Fetching Group from String
}

for(String str:matchList) {
   System.out.println(str);
}

OUTPUT

输出

Java
.NET


What does \\((.+?)\\)mean?

什么\\((.+?)\\)意思?

This regular Expression pattern will start from \\(which will match (as it is reserved in regExp so we need escape this character,same thing for \\)and (.*?)will match any character zero or more time anything moreover in ()considered as Group which we are finding.

这个正则表达式模式将从\\(它将匹配,(因为它在 regExp 中被保留,所以我们需要转义这个字符,同样的事情,\\)并且(.*?)将匹配任何字符零次或多次,而且()被认为是我们正在查找的组。

回答by Yury

I know this was asked 3 years ago, but for anyone with the same/similar question that lands here (as I did), there is something even simpler than using regex:

我知道这是 3 年前提出的问题,但是对于任何遇到相同/类似问题的人(就像我所做的那样),还有比使用正则表达式更简单的方法:

String result = StringUtils.substringBetween(str, "(", ")");

In your example, resultwould be returned as "Java". I would recommend the StringUtils library for various kinds of (relatively simple) string manipulation; it handles things like null inputs automatically, which can be convenient.

在您的示例中,result将返回为“Java”。我会推荐 StringUtils 库用于各种(相对简单的)字符串操作;它会自动处理诸如空输入之类的事情,这很方便。

Documentation for substringBetween(): https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#substringBetween-java.lang.String-java.lang.String-java.lang.String-

substringBetween() 的文档:https: //commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#substringBetween-java.lang.String-java.lang.String- java.lang.String-

There are two other versions of this function, depending on whether the opening and closing delimiters are the same, and whether the delimiter(s) occur(s) in the target string multiple times.

此函数还有另外两个版本,具体取决于开始和结束分隔符是否相同,以及分隔符是否在目标字符串中多次出现。