在C#中,正则表达式是一种模式,用于解析和检查给定的输入文本是否与给定的模式匹配。在C#中,正则表达式通常称为C#正则表达式。 .Net Framework提供了允许模式匹配的正则表达式引擎。模式可以包含任何字符字面量,运算符或构造函数。
C#提供了一个称为Regex的类,可以在System.Text.RegularExpression命名空间中找到该类。此类将执行两件事:
- 解析正则表达式模式的输入文本。
- 确定给定文本中的正则表达式模式。
示例1:下面的示例演示在移动电话号码验证中使用正则表达式。假设您正在制作一个需要验证用户输入的手机号码的表格,然后可以使用正则表达式。
// C# program to validate the Mobile
// Number using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main(string[] args)
{
// Input strings to Match
// valid mobile number
string[] str = {"9925612824",
"8238783138", "02812451830"};
foreach(string s in str)
{
Console.WriteLine("{0} {1} a valid mobile number.", s,
isValidMobileNumber(s) ? "is" : "is not");
}
Console.ReadKey();
}
// method containing the regex
public static bool isValidMobileNumber(string inputMobileNumber)
{
string strRegex = @"(^[0-9]{10}$)|(^\+[0-9]{2}\s+[0-9]
{2}[0-9]{8}$)|(^[0-9]{3}-[0-9]{4}-[0-9]{4}$)";
// Class Regex Repesents an
// immutable regular expression.
// Format Pattern
// xxxxxxxxxx ^[0 - 9]{ 10}$
// +xx xx xxxxxxxx ^\+[0 - 9]{ 2}\s +[0 - 9]{ 2}\s +[0 - 9]{ 8}$
// xxx - xxxx - xxxx ^[0 - 9]{ 3} -[0 - 9]{ 4}-[0 - 9]{ 4}$
Regex re = new Regex(strRegex);
// The IsMatch method is used to validate
// a string or to ensure that a string
// conforms to a particular pattern.
if (re.IsMatch(inputMobileNumber))
return (true);
else
return (false);
}
}
输出:
9925612824 is a valid mobile number.
8238783138 is a valid mobile number.
02812451830 is not a valid mobile number.
示例2:以下示例演示了正则表达式在电子邮件ID验证中的使用。假设您正在制作一个表单,需要验证用户输入的电子邮件ID,然后可以使用正则表达式。
// C# program to validate the Email
// ID using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main(string[] args)
{
// Input strings for Match
// valid E-mail address.
string[] str = {"parth@gmail.com",
"parthmaniyargmail.com",
"@gmail.com"};
foreach(string s in str)
{
Console.WriteLine("{0} {1} a valid E-mail address.", s,
isValidEmail(s) ? "is" : "is not");
}
}
// Method to check the Email ID
public static bool isValidEmail(string inputEmail)
{
// This Pattern is use to verify the email
string strRegex = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";
Regex re = new Regex(strRegex, RegexOptions.IgnoreCase);
if (re.IsMatch(inputEmail))
return (true);
else
return (false);
}
}
输出:
parth@gmail.com is a valid E-mail address.
parthmaniyargmail.com is not a valid E-mail address.
@gmail.com is not a valid E-mail address.
正则表达式语法
有许多基本语法,例如量词,特殊字符,字符类,分组和替代项都用于正则表达式。
量词:
Sub-expression(Greedy) | Sub-expression(Lazy) | Matches |
---|---|---|
* | *? | Used to match the preceding character zero or more times. |
+ | +? | Used to match the preceding character one or more times. |
? | ?? | Used to match the preceding character zero or one time. |
{n} | {n}? | Used to match the preceding character exactly n times. |
{n, } | {n, }? | Used to match the preceding character at least n times. |
{n, m} | {n, m}? | Used to match the preceding character from n to m times. |
范例1:
// C# program to demonstrate
// the * Quantifier
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
public static void Main(string[] args)
{
// This will return any
// pattern b, ab, aab, ...
Regex regex = new Regex(@"a*b");
Match match = regex.Match("aaaabcd");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: aaaab
范例2:
// C# program to demonstrate
// the + Quantifier
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
public static void Main()
{
// this will return any pattern
// like ab, aab, aaab, ....
Regex regex = new Regex(@"a+b");
Match match = regex.Match("aaabcd");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: aaab
范例3:
// C# program to demonstrate
// the ? Quantifier
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main()
{
// This return any pattern like b, ab
Regex regex = new Regex(@"a?b");
Match match = regex.Match("aaaabcd");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: ab
特殊字符
Sub-expression | Matches |
---|---|
^ | Word after this element matches at the beginning of the string or line. |
$ | Word before this element matches at the end of the line or string. |
.(Dot) | Matches any character only once expect \n(new line). |
\d | It is use to match the digit character. |
\D | It is use to match the non-digit character. |
\w | It is use to match any alphanumeric and underscore character. |
\W | It is use to match the any non-word character. |
\s | It is use to match the white-space characters. |
\S | It is use to match the non white-space characters. |
\n | It is use to match a newline character. |
范例1:
// C# program to demonstrate
// the ^ Special Character
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main()
{
// This will return if shyam exist
// at the beginning of the line
Regex regex = new Regex(@"^Shyam");
Match match = regex.Match("Shyam is my pet name");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: Shyam
范例2:
// C# program to demonstrate
// the $ Special Character
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
public static void Main()
{
// This return parth if it
// exist at the end of the line
Regex regex = new Regex(@"Parth$");
Match match = regex.Match("My name is Parth");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: Parth
范例3:
// C# program to demonstrate
// the .(Dot) Special Character
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main()
{
// This will return any word which
// contains only one letter between
// s and t
Regex regex = new Regex(@"s..t");
Match match = regex.Match("This is my seat");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: seat
范例4:
// C# program to demonstrate
// the \d Special Character
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main()
{
// This will the return
// the one digit character
Regex regex = new Regex(@"\d");
Match match = regex.Match("I am 19 years old");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: 1
字符类
Sub-expression | Matches |
---|---|
[] | It is used to match the range of character |
[a-z] | It is used to match any character in the range of a-z. |
[^a-z] | It is used to match any character not in the range of a-z. |
\ | It is used to match Escaped special character. |
范例1:
// C# program to demonstrate
// the [] character class
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main()
{
// This will return one character either
// a or b or c which will come first
Regex regex = new Regex(@"[abc]");
Match match = regex.Match("abcdef");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: a
范例2:
// C# program to demonstrate
// the [a-z] character class
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main()
{
// This will return any character
// between x and z inclusive
Regex regex = new Regex(@"[x-z]");
Match match = regex.Match("xmax");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: x
范例3:
// C# program to demonstrate
// the [^a-z] character class
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main()
{
// This will return other x,
// y and z character
Regex regex = new Regex(@"[^x-z]");
Match match = regex.Match("xmax");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: m
分组和替代
Sub-expression | Matches |
---|---|
() | It is used for group expression |
(a|b) | | Operator is used for alternative either a or b. |
(?(exp) yes|no) | If expression is matched it gives yes otherwise it gives no. |
范例1:
// C# program to demonstrate
// the grouping in regex
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main()
{
// This will return pattern
// will cd, cdcd, cdcdcd, ...
Regex regex = new Regex(@"(cd)+");
Match match = regex.Match("cdcdde");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: cdcd
范例2:
// C# program to demonstrate
// the grouping in regex
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main()
{
// This will either d or e
// which ever comes first
Regex regex = new Regex(@"d|e");
Match match = regex.Match("edge");
if (match.Success)
{
Console.WriteLine("Match Value: " + match.Value);
}
}
}
输出:
Match Value: e