Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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 如何检查字符串只有数字和两个特殊字符_Regex_C# 4.0 - Fatal编程技术网

Regex 如何检查字符串只有数字和两个特殊字符

Regex 如何检查字符串只有数字和两个特殊字符,regex,c#-4.0,Regex,C# 4.0,若字符串中的数字带有。(点)或-(连字符减号),则我希望结果为true,否则为false 我只能通过Regex.IsMatch(输入@“^\d+$”检查数字,但我想检查。(点)和-(连字符减号) 预期O/p: Regex.IsMatch(input, @"^[\d-\.]+$") //this code works for below conditions only if string v1="10-20-30"; //true if string v1="10-20"

若字符串中的数字带有。(点)或-(连字符减号),则我希望结果为true,否则为false

我只能通过
Regex.IsMatch(输入@“^\d+$”
检查数字,但我想检查。(点)和-(连字符减号)

预期O/p:

 Regex.IsMatch(input, @"^[\d-\.]+$")
   //this code works for below conditions only
    if string v1="10-20-30";  //true
    if string v1="10-20";  //true
    if string v1="10.20";  //true
    if string v1="10R20";  //false
    if string v1="10@20";  //false
    if string v1="10-20.30.40-50";  //true
    if string v1="10";  //true

    code not works for below conditions
    if string v1="10--20.30"; //false
    if string v1="10-20-30..";  //false
    if string v1="--10-20.30";  //false
    if string v1="-10-20.30";  //false
    if string v1="10-20.30.";  //false

你可以给一组字符来匹配

Regex.IsMatch(input, @"^[\d-\.]+$")

根据给定的
True/False
关系,您似乎只关心字符集,而不关心顺序。您可以使用仅包含这三种字符类型的字符类来处理此问题

Regex.IsMatch(input, @"^[-0-9.]+$")
似乎您想要“选择性匹配”,我不知道C#中的正则表达式语法,但知道Perl语言中的正则表达式语法。您的要求可通过以下方式实现:
$some_string=~/^\d+(-)\d+/,您可以在C#的正则表达式中搜索有关“选择性匹配”的更多信息

您的第二个和第三个示例似乎表明
-
都是可以的,您不需要这两个。对吗?您也没有提到既没有
也没有
-
的情况。应该有多少个独立的数字?是的,没有必要。或者-你在场。可能只会有数字。