📄 program.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace RegexUsage
{
class Program
{
static void Main(string[] args)
{
Regex regex = new Regex("ABC");
// IsMatch()
Console.WriteLine(regex.IsMatch("Abc"));
Console.WriteLine(regex.IsMatch("tell me ABC"));
Console.WriteLine(regex.IsMatch("tell me Abc"));
Console.WriteLine(Regex.IsMatch("ABC", "Abc", RegexOptions.IgnoreCase));
// Replace()
String mString = "Two little black birds sitting in the tree";
mString = Regex.Replace(mString, "in", "on");
Console.WriteLine(mString);
String stringNumber = "123,456,123,123,789,123,678";
Regex regex2 = new Regex("123");
stringNumber = regex2.Replace(stringNumber, "***", 2, 4);
Console.WriteLine(stringNumber);
// Split()
String mStringMix = "123,ABC,456,DEF,789";
String[] splitResults;
splitResults = Regex.Split(mStringMix, ",");
StringBuilder stringResults = new StringBuilder(32);
foreach (String strEle in splitResults)
{
stringResults.Append(strEle + "\n");
}
Console.WriteLine(stringResults.ToString());
// Match
String intputString = "A sailor want to sea to sea, to see what he could see could see";
Regex regex3 = new Regex("se");
Match matchMade = regex3.Match(intputString, 0);
while (matchMade.Success)
{
Console.WriteLine(matchMade.Value);
matchMade = matchMade.NextMatch();
}
Console.WriteLine("*************");
Regex regex4 = new Regex("se.");
Match matchMade2 = regex4.Match(intputString, 0);
while (matchMade2.Success)
{
Console.WriteLine(matchMade2.Value);
matchMade2 = matchMade2.NextMatch();
}
// Matches
Console.WriteLine("*********** Matches ***********");
MatchCollection matches = Regex.Matches(intputString, "se.");
if (matches.Count >= 3)
{
Console.WriteLine(matches[2].Value);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -