using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Regexp
{
// 測試主程式
static void Main(string[] args)
{
List<String> list = Regexp.matches(@" 32.4 + 56.7 is 89.1 ", @"[0-9]+\.[0-9]+", 0);
foreach (String token in list)
Console.WriteLine(token);
}
// 傳回text 中符合正規表示式pattern 的所有段落。
public static List<String> matches(String text, String pattern, int groupId)
{
List<String> rzList = new List<String>();
Match match = Regex.Match(text, pattern);
for (int i = 0; match.Success; i++)
{
rzList.Add(match.Groups[groupId].Value);
match = match.NextMatch();
}
return rzList;
}
}
執行結果
D:\ExampleCode>csc Regexp.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.