Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Can';无法从字符串中找到matcher.group(1)_Regex - Fatal编程技术网

Regex Can';无法从字符串中找到matcher.group(1)

Regex Can';无法从字符串中找到matcher.group(1),regex,Regex,这是字符串: Not enough money for withdraw of 140.82 USD. Need to fulfill bonuses of 139.82 USD. Current withdrawable amount: 2.33 USD 这是我正在使用的模式,但我需要一个值matcher.group(1)。谁能给我最后一个值2.33美元 任何人都可以帮助纠正我的模式。只需添加与捕获组中最后一个数字匹配的模式即可 pattern.compile("Not enough mon

这是字符串:

Not enough money for withdraw of 140.82 USD. Need to fulfill bonuses of 139.82 USD. Current withdrawable amount: 2.33 USD
这是我正在使用的模式,但我需要一个值
matcher.group(1)
。谁能给我最后一个值
2.33美元


任何人都可以帮助纠正我的模式。

只需添加与捕获组中最后一个数字匹配的模式即可

pattern.compile("Not enough money for withdraw of \\d+(?:\\.\\d+)(?:\\s[A-Z]{3})?\\. Need to fulfill bonuses of \\d+(?:\\.\\d+)(?:\\s[A-Z]{3})?\\. Current withdrawable amount: (\\d+(?:\\.\\d+)?(?:\\s[A-Z]{3})?)");
                                                                                                                                                                                 ^                                ^
此外,您还需要将模式中的
信用
更改为
货币


您通常的正则表达式
\d+(?:\\.\\d+(:\\s[A-Z]{3})\\.
对最后一组不起作用,原因很简单,因为它不会在一个时段内结束。(


\d+(?:\\.\\d+(:\\s[A-Z]{3})
应该可以使用。

如果您知道字符串具有特定格式,则不需要完全匹配整个字符串,只需匹配所需的部分,只需一行代码

最简单的方法是

String withdrawable = str.replaceAll(".*?([.\\d]+) USD$", "$1");
这是因为目标位于输入的末尾。对于更一般和更具表现力的方法:

String withdrawable = str.replaceAll(".*withdrawable amount: ([.\\d]+).*", "$1");

您只有非捕获组。从要捕获的组中删除
?:
将图案结尾替换为
(\\d+(?:\\\.\\d+)(?:\\s[A-Z]{3})
String withdrawable = str.replaceAll(".*withdrawable amount: ([.\\d]+).*", "$1");