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

时间:2022-09-13 00:10:10

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

如何使用模式匹配或其他方法从“(”和“)”中提取字符串。例如,如果文本是

"Hello (Java)"

“你好(Java)”

Then how to get only "Java".

那么如何只获得“Java”。

Thanks.

谢谢。

3 个解决方案

#1


38  

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

#2


20  

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中被保留,所以我们需要转义这个字符,\也是一样的)和(.*?)将匹配任何字符0或更多的时间,而且在()中还有()作为我们正在查找的组。

#3


4  

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:

我知道这是三年前的问题,但对于任何有相同/类似问题的人(就像我一样),有比使用regex更简单的东西:

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

In your example, result would 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.

在您的示例中,结果将返回为“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.

这个函数有两个其他的版本,这取决于开闭定界符是否相同,以及分隔符是否在目标字符串中多次出现。

#1


38  

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

#2


20  

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中被保留,所以我们需要转义这个字符,\也是一样的)和(.*?)将匹配任何字符0或更多的时间,而且在()中还有()作为我们正在查找的组。

#3


4  

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:

我知道这是三年前的问题,但对于任何有相同/类似问题的人(就像我一样),有比使用regex更简单的东西:

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

In your example, result would 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.

在您的示例中,结果将返回为“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.

这个函数有两个其他的版本,这取决于开闭定界符是否相同,以及分隔符是否在目标字符串中多次出现。