我无法找到匹配此模式的正则表达式:
.
或,
),后跟-
,然后是.
或,
),后跟此模式可以重复一次或多次。
以下示例应与正则表达式匹配:
1-2;
1-2;3-4;5-6;
1,0-2;
1.0-2;
1,0-2.0;
1-2 3-4;
1-2 3,00-4;5.0-6;
以下示例不应与正则表达式匹配:
1-2
1 2;
1_2;
1-2;3-4
答案 0 :(得分:2)
修改根据1 2;
移动到不匹配进行了更新。
这应该有效:
@"^(\d+([,.]\d+)?-\d+([,.]\d+)?[ ;])+(?<=;)$"
解释
^ //Start of the string.
( //Start of group to be repeated. You can also use (?=
\d+ //One or more digits.
([,.]\d+)? //With an optional decimal
- //Separated by a dash
\d+([,.]\d+)? //Same as before.
[ ;] //Terminated by a semi-colon or a space
)+ //One or more of these groups.
(?<=;) //The last char before the end needs to be a semi-colon
$ //End of string.
答案 1 :(得分:1)
试试这个:
@"^([\d.,]+-[\d.,]+[ ;])*[\d.,]+-[\d.,]+;$"
请注意,[\d.,]+
接受一些通常不被视为有效“数字”值的字符序列,例如00..,.,
。您可能希望找到更好的正则表达式来匹配数值并将其替换为正则表达式。