当前位置: 首页>>代码示例>>C#>>正文


C# Regex.IsMatch方法代码示例

本文整理汇总了C#中System.Text.RegularExpressions.Regex.IsMatch方法的典型用法代码示例。如果您正苦于以下问题:C# Regex.IsMatch方法的具体用法?C# Regex.IsMatch怎么用?C# Regex.IsMatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Text.RegularExpressions.Regex的用法示例。


在下文中一共展示了Regex.IsMatch方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string[] partNumbers= { "1298-673-4192", "A08Z-931-468A", 
                              "_A90-123-129X", "12345-KKA-1230", 
                              "0919-2893-1256" };
      Regex rgx = new Regex(@"^[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$");
      foreach (string partNumber in partNumbers)
         Console.WriteLine("{0} {1} a valid part number.", 
                           partNumber, 
                           rgx.IsMatch(partNumber) ? "is" : "is not");
   }
}
开发者ID:.NET开发者,项目名称:System.Text.RegularExpressions,代码行数:18,代码来源:Regex.IsMatch

输出:

1298-673-4192 is a valid part number.
A08Z-931-468A is a valid part number.
_A90-123-129X is not a valid part number.
12345-KKA-1230 is not a valid part number.
0919-2893-1256 is not a valid part number.

示例2: Main

//引入命名空间
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string[] partNumbers= { "Part Number: 1298-673-4192", "Part No: A08Z-931-468A", 
                              "_A90-123-129X", "123K-000-1230", 
                              "SKU: 0919-2893-1256" };
      Regex rgx = new Regex(@"[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$");
      foreach (string partNumber in partNumbers)
      {
         int start = partNumber.IndexOf(':');
         if (start >= 0)
         {
            Console.WriteLine("{0} {1} a valid part number.", 
                              partNumber, 
                              rgx.IsMatch(partNumber, start) ? "is" : "is not");
         }
         else
         {
            Console.WriteLine("Cannot find starting position in {0}.", partNumber);
         }
      }
   }
}
开发者ID:.NET开发者,项目名称:System.Text.RegularExpressions,代码行数:28,代码来源:Regex.IsMatch

输出:

Part Number: 1298-673-4192 is a valid part number.
Part No: A08Z-931-468A is a valid part number.
Cannot find starting position in _A90-123-129X.
Cannot find starting position in 123K-000-1230.
SKU: 0919-2893-1256 is not a valid part number.

示例3: Main

//引入命名空间
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string[] partNumbers= { "1298-673-4192", "A08Z-931-468A", 
                              "_A90-123-129X", "12345-KKA-1230", 
                              "0919-2893-1256" };
      string pattern = @"^[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$";
      foreach (string partNumber in partNumbers)
         Console.WriteLine("{0} {1} a valid part number.", 
                           partNumber, 
                           Regex.IsMatch(partNumber, pattern) ? "is" : "is not");
   }
}
开发者ID:.NET开发者,项目名称:System.Text.RegularExpressions,代码行数:18,代码来源:Regex.IsMatch

输出:

1298-673-4192 is a valid part number.
A08Z-931-468A is a valid part number.
_A90-123-129X is not a valid part number.
12345-KKA-1230 is not a valid part number.
0919-2893-1256 is not a valid part number.

示例4: Main

//引入命名空间
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string[] partNumbers= { "1298-673-4192", "A08Z-931-468a", 
                              "_A90-123-129X", "12345-KKA-1230", 
                              "0919-2893-1256" };
      string pattern = @"^[A-Z0-9]\d{2}[A-Z0-9](-\d{3}){2}[A-Z0-9]$";
      foreach (string partNumber in partNumbers)
         Console.WriteLine("{0} {1} a valid part number.", 
                           partNumber, 
                           Regex.IsMatch(partNumber, pattern, RegexOptions.IgnoreCase) 
                                         ? "is" : "is not");
   }
}
开发者ID:.NET开发者,项目名称:System.Text.RegularExpressions,代码行数:19,代码来源:Regex.IsMatch

输出:

1298-673-4192 is a valid part number.
A08Z-931-468a is a valid part number.
_A90-123-129X is not a valid part number.
12345-KKA-1230 is not a valid part number.
0919-2893-1256 is not a valid part number.

示例5: Main

//引入命名空间
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string[] partNumbers= { "1298-673-4192", "A08Z-931-468a", 
                              "_A90-123-129X", "12345-KKA-1230", 
                              "0919-2893-1256" };
      string pattern = @"^[A-Z0-9]\d{2}[A-Z0-9](-\d{3}){2}[A-Z0-9]$";
      foreach (string partNumber in partNumbers)
         try {
            Console.WriteLine("{0} {1} a valid part number.", 
                              partNumber, 
                              Regex.IsMatch(partNumber, pattern, RegexOptions.IgnoreCase) 
                                            ? "is" : "is not", TimeSpan.FromMilliseconds(500));
         }
         catch (RegexMatchTimeoutException e) {
            Console.WriteLine("Timeout after {0} seconds matching {1}.",
                              e.MatchTimeout, e.Input);
         }
   }
}
开发者ID:.NET开发者,项目名称:System.Text.RegularExpressions,代码行数:25,代码来源:Regex.IsMatch

输出:

1298-673-4192 is a valid part number.
A08Z-931-468a is a valid part number.
_A90-123-129X is not a valid part number.
12345-KKA-1230 is not a valid part number.
0919-2893-1256 is not a valid part number.

示例6: Main

//引入命名空间
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;

public class MainClass{

   public static void Main(){
        Regex r = new Regex("^\\d+");
        
        
        string[] ss = new string[] { "123abc", "abc123", "123", "abc", "a123bc" };

        foreach (string s in ss)
           Console.WriteLine(r.IsMatch(s));
   }
}
开发者ID:C#程序员,项目名称:System.Text.RegularExpressions,代码行数:20,代码来源:Regex.IsMatch


注:本文中的System.Text.RegularExpressions.Regex.IsMatch方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。