ex-10-08
来自「Programming Csharp Source Code(代码) Prog」· 代码 · 共 42 行
TXT
42 行
// 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 + =
减小字号Ctrl + -
显示快捷键?