当谈到正则表达式时我完全迷失了。 我得到生成的字符串,例如:
Your number is (123,456,789)
如何过滤掉 123,456,789
?
最佳答案
您可以使用此正则表达式来提取包括逗号的数字
\(([\d,]*)\)
第一个捕获的组将有你的比赛。代码如下所示
String subjectString = "Your number is (123,456,789)";
Pattern regex = Pattern.compile("\\(([\\d,]*)\\)");
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
String resultString = regexMatcher.group(1);
System.out.println(resultString);
}
正则表达式的解释
"\\(" + // Match the character “(” literally
"(" + // Match the regular expression below and capture its match into backreference number 1
"[\\d,]" + // Match a single character present in the list below
// A single digit 0..9
// The character “,”
"*" + // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
")" +
"\\)" // Match the character “)” literally
关于Java与正则表达式、子串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8133792/