Java与正则表达式、子串

标签 java regex substring

当谈到正则表达式时我完全迷失了。 我得到生成的字符串,例如:

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

这将帮助您开始 http://www.regular-expressions.info/reference.html

关于Java与正则表达式、子串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8133792/

相关文章:

android - Kotlin:如何获取字符串中 "@"之后的字符?

java - 缓存对象并在jsf中使用它们

python - sre_constants.error : nothing to repeat in jython

python - 使用 Regex 观看第一场比赛 - PYTHON

javascript - 在 Javascript 中使用正则表达式删除 HTML 注释

c++ - 剪切带有多个结束符的字符串

java - 如何在单击新框架时退出父/祖先框架

java - Netbeans 项目转移 7.1.1 至 7.4

java - 使用 Spring Vault 写入对象返回 Status 400 : no data provided

string - 如何用随机替换词有效地替换字符串中的词?