📄 ex-10-08
字号:
// Example 10-08: Using the Group class
namespace Programming_CSharp
{
using System;
using System.Text.RegularExpressions;
class Test
{
public static void Main()
{
string string1 = "04:03:27 127.0.0.0 LibertyAssociates.com";
// group time = one or more digits or colons followed by space
Regex theReg = new Regex(@"(?<time>(\d|\:)+)\s" +
// ip address = one or more digits or dots followed by space
@"(?<ip>(\d|\.)+)\s" +
// site = one or more characters
@"(?<site>\S+)");
// get the collection of matches
MatchCollection theMatches = theReg.Matches(string1);
// iterate through the collection
foreach (Match theMatch in theMatches)
{
if (theMatch.Length != 0)
{
Console.WriteLine("\ntheMatch: {0}",
theMatch.ToString());
Console.WriteLine("time: {0}",
theMatch.Groups["time"]);
Console.WriteLine("ip: {0}",
theMatch.Groups["ip"]);
Console.WriteLine("site: {0}",
theMatch.Groups["site"]);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -