我想使用java中的正则表达式从文本中提取数字(连同括号) 例如:(234)
答案 0 :(得分:1)
此模式应匹配:
\(\d+\)
因为你没有提到任何要求,所以我认为接受任何数量的数字。只有数字。 在你想要获得的数字之间不能有其他符号,然后是数字。
答案 1 :(得分:1)
这个问题相当普遍,但无论如何我都会尝试给出答案。
提供以下字符串:This is an example (123) string (234)
摘录:(123)
和(234)
你可以使用java.util.regex
这样做:
import java.util.regex.*;
...
Pattern p = Pattern.compile("(\\(\\d+\\))");
Matcher m = p.matcher("This is an example (123) string (234)");
while (m.find()) {
System.out.println("Found: " + m.group(1));
}
上面的代码应该打印:
Found: (123)
Found: (234)