Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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 在groovy中使用正则表达式从字符串中提取数字_Regex_Groovy_Extract - Fatal编程技术网

Regex 在groovy中使用正则表达式从字符串中提取数字

Regex 在groovy中使用正则表达式从字符串中提取数字,regex,groovy,extract,Regex,Groovy,Extract,我有以下类型的字符串,其中包含数字: (12 - 17) (4.5 - 5.5) (4 - 10) 我的代码适用于前两个示例,如下所示: def numbers=range=~/\d{1,3}.?\d{1,2}?/ 其中,数字的结果为: [12,17] [4.5,5.5] 但对于最后一个,这仅仅是一个问题 [10] 它没有得到4 有人知道我错在哪里吗?您的正则表达式末尾至少需要2个整数。查找:\d{1,3}匹配1到3位数字。?匹配除换行符1或0之外的任何字符,可选和\d{1

我有以下类型的字符串,其中包含数字:

(12  -   17)
(4.5  - 5.5)
(4    -  10)
我的代码适用于前两个示例,如下所示:

def numbers=range=~/\d{1,3}.?\d{1,2}?/
其中,数字的结果为:

[12,17]
[4.5,5.5]
但对于最后一个,这仅仅是一个问题 [10] 它没有得到4


有人知道我错在哪里吗?

您的正则表达式末尾至少需要2个整数。查找:\d{1,3}匹配1到3位数字。?匹配除换行符1或0之外的任何字符,可选和\d{1,2}?匹配{1,2}的1或2个数字?是限制量词的惰性版本,意味着它将匹配尽可能少的数字以返回有效匹配

使用

说明:

\d{1,3}-1到3位数字 ?:\。\d{1,2}?-1个或0个序列,原因是什么?其中: \-文字句号 \这是限制量词的贪婪版本。 以下是一份:

输出:[12,17,4.5,5.5,4,10]

/\d{1,3}(?:\.\d{1,2})?/
def x = "(12  -   17)(4.5  - 5.5)(4    -  10)"
def res = x.findAll(/\d{1,3}(?:\.\d{1,2})?/)
println res