[英]Optional group in Regex returns too many matches
I want to match only values (ie 2.699.230,20) from an input on a C# Regex class. 我只想从C#Regex类的输入中匹配值(即2.699.230,20)。
I use "(\\\\.?[0-9]){2,}\\\\,[0-9]{2}"
and it matches desired values 5.000,00
, 2.699.230,20
, 1.000.000,00
, etc. The {2,}
is to only match values above 999,99
. 我用
"(\\\\.?[0-9]){2,}\\\\,[0-9]{2}"
和它的期望值相匹配5.000,00
, 2.699.230,20
, 1.000.000,00
,等等。 {2,}
仅匹配999,99
以上的999,99
。
But there's also other values on the same input that i want to match. 但是在同一输入上还有其他要匹配的值。 They are always
1.000
or above, but the difference is that it don't have the decimal ,00
part. 它们始终为
1.000
或更高,但是区别在于它没有小数部分的,00
。 Examples: 4.541.087
, 8.997.434
. 例如:
4.541.087
, 8.997.434
。
So i put the last part of the regex a binary (0 or 1 times present) option (added (...)?
around the decimal part: 所以我将正则表达式的最后一部分放在二进制(出现0或1次)选项(添加
(...)?
"(\\\\.?[0-9]){2,}(\\\\,[0-9]{2})?"
, but now this matches hundreds of numbers, including 18
, 1.0
, 1.5.2
, 8854
, etc. 但现在这个匹配上百号,包括
18
, 1.0
, 1.5.2
, 8854
,等等。
So, how can i make the decimal part optional, so it matches both 1.000,00
and 1.000
? 那么,我如何使小数部分为可选,使其与
1.000,00
和1.000
匹配?
It seems you only want to get numbers that have a .
看来您只想取得具有的数字
.
as thousand separator in them with optional 2 digits in the fractional part. 作为其中的千位分隔符,小数部分中的可选2位数字。
Use 采用
@"\b\d{1,3}(?:\.\d{3})+(?:,\d{2})?\b"
See the regex demo . 参见regex演示 。
Details : 详细资料 :
\\b
- leading word boundary (may be replaced with (?<!\\d)
negative lookbehind to disallow only digit before...) \\b
前导词边界(可以用(?<!\\d)
后面的负数代替,以仅允许前面的数字...) \\d{1,3}
- 1 to 3 digits \\d{1,3}
-1至3位数字 (?:\\.\\d{3})+
- 1 or more sequences of a dot and 3 digits ( NOTE : if you change +
with *
, it will match values below 1.000
) (?:\\.\\d{3})+
-1个或多个点和3位数字的序列( 注意 :如果用*
更改+
,它将匹配小于1.000
值) (?:,\\d{2})?
- an optional sequence of a ,
and 2 digits. ,
和2位数字。 \\b
- trailing word boundary (may be replaced with (?!\\d)
negative lookbahead to disallow only digit after the number). \\b
尾随单词边界(可以用(?!\\d)
负数lookbahead代替,以仅允许数字后面的数字)。 var re = @"\b\d{1,3}(?:\.\d{3})+(?:,\d{2})?\b";
var str = "values 5.000,00, 2.699.230,20, 1.000.000,00, etc. 999,99 including 18, 1.0, 1.5.2, 8854, etc";
var res = Regex.Matches(str, re)
.Cast<Match>()
.Select(p => p.Value)
.ToList();
Console.WriteLine(string.Join("\n", res));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.