本文整理汇总了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");
}
}
输出:
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);
}
}
}
}
输出:
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");
}
}
输出:
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");
}
}
输出:
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);
}
}
}
输出:
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));
}
}