本文整理匯總了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));
}
}