Regex

Регулярні вирази

using System.Text.RegularExpressions;

1. 

string s = "Sir, in my heart there was a kind of fighting. That would not let me sleep.";

MatchCollection m = Regex.Matches(s, "e.");

for (int i=0; i<m.Count; i++) {

     Console.Write(m[i].Value + " ");      // ea er e  et e  ee

}

2.

string s = "Hello Bill Gates";

Regex regex = new Regex("ll");                   // маска

MatchCollection m1 = regex.Matches(s);      // зібрати співпадіння в колекцію

Console.WriteLine(m1.Count.ToString());     // 2

3.

string s = "Hello    Bill  Gates";

Console.WriteLine(new Regex(@"\s+").Replace(s," "));   // "Hello Bill Gates" - заміна по масці

4.

string s = "<txt>Новозеландський долар</txt><rate>15.939458</rate><cc>NZD</cc>";

Match m = Regex.Match(s, "<rate>(.*?)</rate><cc>NZD</cc>");     // (.*?) - група

Console.WriteLine(m.Groups[1].Value);               // 15.939458               [1] - перше попадання