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,我想检查输入字符串的格式是否正确^[\d-.]+$表达式只检查数字和。(点)和-(减)的存在性,但我还想检查其序列 假设我想用计算器。而且-仅此而已。如何得到满足以下所有条件的正则表达式 Regex.IsMatch(input, @"^[\d-\.]+$") //this expression works for below conditions only if string v1="10-20-30"; // should return true if string v

我想检查输入字符串的格式是否正确^[\d-.]+$表达式只检查数字和。(点)和-(减)的存在性,但我还想检查其序列

假设我想用计算器。而且-仅此而已。如何得到满足以下所有条件的正则表达式

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

    //above expression not works for below conditions
    if string v1="10--20.30"; // should return false
    if string v1="10-20-30..";  // should return false
    if string v1="--10-20.30";  // should return false
    if string v1="-10-20.30";  // should return false
    if string v1="10-20.30.";  // should return false
大概是

        var pattern = @"^(\d+(-|\.))*\d+$";
我应该为你做这项工作

这个正则表达式的“意思”是:

  • 查找一个或多个数字(\d+)
  • 后跟减号或点(-)-需要用\
  • 这可能是字符串中的0次或更多次-结尾的星号(\d+(-))*
  • 然后是另一个或多个数字(\d+)
  • 所有这些都应该在字符串开头之后和结尾之前(我相信您已经知道了^and$)
  • 注意:如果需要使数字可能为负数,则需要在正则表达式中的两个\d+实例之前添加另一个条件减号,或者:

    var模式=@“^(-d+(-124;)*-?\ d+$”


    问候像魅力一样有效。很好的解释,谢谢