gpt4 book ai didi

Java与正则表达式、子串

转载 作者:行者123 更新时间:2023-12-01 05:36:24 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com