C# 正则表达式
C# 正则表达式
在 C# 中,正则表达式用于解析和验证给定文本以匹配定义的模式(例如,电子邮件地址)。模式可以包含运算符、字符字面量或构造。
在.NET Framework 中使用正则表达式处理文本,通常我们使用正则表达式引擎。在 C# 中,正则表达式由 Regex 表示。
正则表达式
我们使用正则表达式来检查给定的字符串是否是否匹配模式。正则表达式或正则表达式是定义模式的字符序列。模式可以包含数字、文字、运算符、字符等。我们使用模式来搜索字符串或文件。在这里,我们将查看是否找到匹配项。
通常,正则表达式用于解析、查找字符串或验证等。
这里我们采用例如,我们可以使用正则表达式来检查社会安全号码、有效出生日期、匹配姓名以逗号分隔的全名、替换子字符串、正确的电子邮件格式、货币格式和
Regex Class
Regex 类展示了.NET Framework 中的正则表达式引擎。 Regex 类用于解析大量文本以查找特定字符模式。我们可以使用 Regex 类来提取、编辑、替换或删除子字符串的文本。
System.Text.RegularExpressions 命名空间包含 Regex 类。正则表达式类将字符串模式作为参数和另一个可选参数。
现在我们将从正则表达式创建一个模式。在此代码中,我们必须将模式与以字符"M"开头的单词匹配。
// Create a pattern for the word that starts with letter "M"
string pattern = @"\b[M]\w+";
// Create a Regex
Regex rgex = new Regex(pattern);
这里我们有一个代码,其中包含我们必须解析的作者姓名的长文本。
// long string
string authors = "Ramesh chand, Rakeshwar";
这里我们将使用 Matches 方法查找返回 MatchCollection 的所有匹配项。
// Get all matches
MatchCollection matchedAuthors = rg.Matches(authors);
To find the matches collection we will use the for loop // Print all matched authors
for (int count = 0; count < matchedAuthors.Count; count++)
Console.WriteLine(matchedAuthors[count]. Value);
现在让我们举个例子来找出字母'M'。
// Create a pattern for a word which starts with letter "M"
string pattern1 = @"\b[M]\w+";
// Create a Regex
Regex rg = new Regex(pattern1);
// long string
string Authors = "Ramesh Chand,Rakeshwar";
// Get all matches
MatchCollection matchedAuthors = rg.Matches(Authors );
// for print all matched authors
for (int count = 0; count < matchedAuthors.Count; count++)
Console.WriteLine(matchedAuthors[count].Value);
从上面的例子中我们试图找到字符'M'。但是,如果单词以小"m"开头,则会出现一种情况。在这种情况下,我们将使用 RegexOption.IgnoreCase 参数,以便 Regex 忽略大写或小写。
// Create a pattern for a word that starts with letter "M"
string pattern1 = @"\b[m]\w+";
// Create a Regex
Regex rgex = new Regex(pattern, RegexOptions.IgnoreCase);
C# 中的正则表达式示例
这里我们举一个例子来验证电子邮件是否格式正确。为此,我们将使用 Regex 类。
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string email = "support@lidihuo.com";
var result = Regex.IsMatch(email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
Console.Write("Is valid: {0} ", result);
Console.ReadLine();
}
}
}
从上面的例子来看,我们正在验证输入的字符串,它的格式是否有效。为此,我们使用 Regex 类。对于输入文本的验证,我们使用了 IsMatch 方法和正则表达式的模式。
执行上述代码后,我们得到了结果,如下所示:
C# 中的正则表达式类方法
要对输入字符串执行各种操作,Regex 类包含不同的方法。该表列出了 C# 中 Regex 的各种方法。
方法 |
说明 |
IsMatch |
我们使用 IsMatch 方法找出给定的输入字符串是否与正则表达式模式匹配。 |
匹配 |
Matches 方法用于返回与正则表达式模式匹配的文本。 |
替换 |
Replace 方法用于替换匹配正则表达式模式的文本。 |
拆分 |
我们使用 Split 方法将字符串拆分为这些位置的子字符串数组,与正则表达式模式匹配。 |
Regex 类的上述方法用于验证、替换或拆分字符串的值与正则表达式模式,这是基于需求的。
C# 中的正则表达式替换字符串示例
借助此示例,我们尝试使用正则表达式模式查找子字符串,该模式替换 C# 中所需的值。
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string str = "Hi,welcome@lidihuo.com";
string result = Regex.Replace(str, "[^a-zA-Z0-9_]+", " ");
Console.Write("{0} ", result);
Console.ReadLine();
}
}
}
在上面的例子中,我们使用了正则表达式。 Replace方法用于通过正则表达式模式("[^a-zA-Z0-9_]+")查找字符串中的特殊字符并用空格替换。
在上面例如,正则表达式("[^a-zA-Z0-9_]+")的模式试图匹配单个字符,该字符未在字符组中定义。
之后执行上面的程序,我们会得到如下输出,如下图:
在正则表达式中查找重复词 C#
通过使用正则表达式模式,我们可以很容易地找到重复词。
在这个例如,我们正在尝试使用 C# 中的 Regex 类方法在给定字符串中查找重复的单词。
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program1
{
static void Main(string[] args)
{
string str1 = "Welcome To to lidihuo. Here we can learn C# easily easily way";
MatchCollection collection = Regex.Matches(str1, @"\b(\w+?)\s\1\b", RegexOptions.IgnoreCase);
foreach (Match m in collection)
{
Console.WriteLine("{0} (duplicates '{1}') at position {2}", m.Value, m.Groups[1].Value, m.Index);
}
Console.ReadLine();
}
}
}
上面的例子使用了正则表达式。 Matches 方法是通过正则表达式模式("\b(\w+?)\s\1\b")找出重复的词。
执行上述后代码,我们会得到如下输出,如下图:
