[英]finding number between 2 parenthesis using regular expression
In a line I may have (123,456)
I want to find it using pattern in java. 在一行中,我可能有(123,456)
我想使用Java中的模式找到它。 What I did is: 我所做的是:
Pattern pattern = Pattern.compile("\\W");
Matcher matcher = pattern.matcher("(");
while (matcher.find()) {
System.out.print("Start index: " + matcher.start());
System.out.print(" End index: " + matcher.end() + " ");
}
Input: This is test (123,456)
Output: Start index: 0 End index: 1 (
Why?? 输入: This is test (123,456)
输出: Start index: 0 End index: 1 (
为什么?
I am not sure how \\W
is going to match it. 我不确定\\W
如何匹配它。 \\W
matches a non word character. \\W
匹配非单词字符。
You will also have to escape those backslashes. 您还必须转义那些反斜杠。
Round brackets need to be escaped , as by default they are used for grouping. 圆括号需要转义,因为默认情况下它们用于分组。
Maybe the regex you meant was 也许你的意思是正则表达式
Pattern pattern = Pattern.compile("\\([,\\d]+\\)");
Matcher matcher = pattern.matcher(inputString);
while (matcher.find()) {
String matched = matcher.group();
//Do something with it
}
Explanation: 说明:
\\( # Match (
[,\\d]+ # Match 1+ digits/commas. Don't be surprised if it matches (,,,,,,)
\\) # Match )
一行完成:
String num = str.replaceAll(".*\\(([\\d,]+)\\).*", "$1");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.