📄 class1.cs
字号:
using System;
using System.Text.RegularExpressions;
namespace Matching
{
class MatchingApp
{
[STAThread]
static void Main(string[] args)
{
// Regex r = new Regex("in"); // same thing
Regex r = new Regex("(in)");
Match m = r.Match("Matching");
if (m.Success)
{
Console.WriteLine(
"Found '{0}' at position {1}",
m.Value, m.Index);
}
Console.WriteLine();
MatchCollection mc = r.Matches(
"The King Was in His Counting House");
for (int i = 0; i < mc.Count; i++)
{
Console.WriteLine(
"Found '{0}' at position {1}",
mc[i].Value, mc[i].Index);
}
Console.WriteLine();
string s2 = "The King Was in His Counting House";
Match m2;
for (m2 = r.Match(s2); m2.Success; m2 = m2.NextMatch())
{
Console.WriteLine(
"Found '{0}' at position {1}",
m2.Value, m2.Index);
}
Console.WriteLine();
Regex q = new Regex(" in ");
MatchCollection mm = q.Matches(
"The King Was in His Counting House");
for (int i = 0; i < mm.Count; i++)
{
Console.WriteLine(
"Found '{0}' at position {1}",
mm[i].Value, mm[i].Index);
}
Console.WriteLine();
Regex p = new Regex("((an)|(in)|(on))");
MatchCollection mn = p.Matches(
"The King Kong Band Wins Again");
for (int i = 0; i < mn.Count; i++)
{
Console.WriteLine(
"Found '{0}' at position {1}",
mn[i].Value, mn[i].Index);
}
Console.WriteLine();
Regex o = new Regex("(a|i|o)n");
MatchCollection mo = o.Matches(
"The King Kong Band Wins Again");
for (int i = 0; i < mo.Count; i++)
{
Console.WriteLine(
"Found '{0}' at position {1}",
mo[i].Value, mo[i].Index);
}
Console.WriteLine();
Regex n = new Regex("Gr(a|e)y");
MatchCollection mp = n.Matches(
"Green, Grey, Granite, Gray");
for (int i = 0; i < mp.Count; i++)
{
Console.WriteLine(
"Found '{0}' at position {1}",
mp[i].Value, mp[i].Index);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -