1. 있나 없나 확인
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; using System.Text.RegularExpressions; namespace Console { class Program { static async Task Main(string[] args) { var words = new List<string>() { "Seven", "even", "Maven", "Amen", "eleven" }; var rx = new Regex(@".even", RegexOptions.Compiled); foreach (string word in words) { if (rx.IsMatch(word)) { Console.WriteLine($"{word} does match"); } else { Console.WriteLine($"{word} does not match"); } } Console.ReadLine(); } } } |
1 2 3 4 5 6 7 |
Seven does match even does not match Maven does not match Amen does not match eleven does match |
2. 찾는 문장 위치 찾기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
var content = @"Foxes are omnivorous mammals belonging to several genera of the family Canidae. Foxes have a flattened skull, upright triangular ears, a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every continent except Antarctica. By far the most common and widespread species of fox is the red fox."; var rx = new Regex("fox(es)?", RegexOptions.Compiled | RegexOptions.IgnoreCase); Match match = rx.Match(content); //Console.WriteLine(match.Value); while (match.Success) { Console.WriteLine($"{match.Value} at index {match.Index}"); match = match.NextMatch(); } Console.ReadLine(); } |
1 2 3 4 5 6 7 |
Foxes at index 0 Foxes at index 82 Foxes at index 198 fox at index 300 fox at index 315 |
3. 단어별로 is 찾기 / Boundary로 is 찾기 / \b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var text = "This island is beautiful"; var rx = new Regex(@"\bis\b", RegexOptions.Compiled); var matches = rx.Matches(text); //Console.WriteLine(match.Value); foreach (Match match in matches) { Console.WriteLine($"{match.Value} at {match.Index}"); } |
1 2 3 |
is at 12 |
4. 단어별로 모두 나누기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var content = @"1Foxes 2 ; are omnivorous mammals belonging to several genera of the family Canidae. Foxes have a flattened skull, upright triangular ears, a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every continent except Antarctica. By far the most common and widespread species of fox is the red fox."; var rx = new Regex(@"\w+", RegexOptions.Compiled | RegexOptions.IgnoreCase); var matches = rx.Matches(content); Console.WriteLine(matches.Count); foreach (var match in matches) { Console.WriteLine(match); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
52 Foxes1 2 are omnivorous mammals belonging to several genera of the family Canidae. Foxes have a flattened skull, upright triangular ears, a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every continent except Antarctica. By far the most common and widespread species of fox is the red fox |
5. 첫문장으로 있나 찾기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Console.OutputEncoding = System.Text.Encoding.UTF8; var sentences = new List<string>() { "I am looking for Jane.", "Jane was walking along the river.", "Kate and Jane are close friends." }; var rx = new Regex(@"^Jane", RegexOptions.Compiled); foreach (string sentence in sentences) { if (rx.IsMatch(sentence)) { Console.WriteLine($"{sentence} does match"); } else { Console.WriteLine($"{sentence} does not match"); } } |
1 2 3 4 5 |
I am looking for Jane.does not match Jane was walking along the river.does match Kate and Jane are close friends.does not match |
6. 선택문장들 하나라도 있나 찾기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var users = new List<string>() {"Jane", "Thomas", "Robert", "Lucy", "Beky", "John", "Peter", "Andy"}; var rx = new Regex("Jane|Beky|Robert", RegexOptions.Compiled); foreach (string user in users) { if (rx.IsMatch(user)) { Console.WriteLine($"{user} does match"); } else { Console.WriteLine($"{user} does not match"); } } |
1 2 3 4 5 6 7 8 9 10 |
Jane does match Thomas does not match Robert does match Lucy does not match Beky does match John does not match Peter does not match Andy does not match |
7. 완전 문장 찾기 / capturing 표시 : ( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var sites = new List<string>() {"webcode.me", "zetcode.com", "freebsd.org", "netbsd.org"}; var rx = new Regex(@"(\w+)\.(\w+)", RegexOptions.Compiled); foreach (var site in sites) { Match match = rx.Match(site); if (match.Success) { Console.WriteLine(match.Value); Console.WriteLine(match.Groups[1]); Console.WriteLine(match.Groups[2]); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
webcode.me webcode me zetcode.com zetcode com freebsd.org freebsd org netbsd.org netbsd org |
8. (Group) 사용하기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
string[] expressions = { "16 + 11", "12 * 5", "27 / 3", "2 - 8" }; string pattern = @"(\d+)\s+([-+*/])\s+(\d+)"; foreach (var expression in expressions) { var rx = new Regex(pattern, RegexOptions.Compiled); var matches = rx.Matches(expression); foreach (Match match in matches) { int val1 = Int32.Parse(match.Groups[1].Value); int val2 = Int32.Parse(match.Groups[3].Value); var oper = match.Groups[2].Value; switch (oper) { case ("+"): Console.WriteLine("{0} = {1}", match.Value, val1 + val2); break; case ("-"): Console.WriteLine("{0} = {1}", match.Value, val1 - val2); break; case ("*"): Console.WriteLine("{0} = {1}", match.Value, val1 * val2); break; case ("/"): Console.WriteLine("{0} = {1}", match.Value, val1 / val2); break; } } |
1 2 3 4 5 6 |
16 + 11 = 27 12 * 5 = 60 27 / 3 = 9 2 - 8 = -6 |
9. (Group) 안에 그룹 (Group)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
string text = "Today is a beautiful day. The sun is shining."; string pattern = @"\b(\w+\s*)+\."; MatchCollection matches = Regex.Matches(text, pattern); foreach (Match match in matches) { Console.WriteLine("Matched sentence: {0}", match.Value); for (int i = 0; i < match.Groups.Count; i++) { Console.WriteLine("\tGroup {0}: {1}", i, match.Groups[i].Value); int captures = 0; foreach (Capture capture in match.Groups[i].Captures) { Console.WriteLine("\t\tCapture {0}: {1}", captures, capture.Value); captures++; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Matched sentence: Today is a beautiful day. Group 0: Today is a beautiful day. Capture 0: Today is a beautiful day. Group 1: day Capture 0: Today Capture 1: is Capture 2: a Capture 3: beautiful Capture 4: day Matched sentence: The sun is shining. Group 0: The sun is shining. Capture 0: The sun is shining. Group 1: shining Capture 0: The Capture 1: sun Capture 2: is Capture 3: shining |
10. 대소문자 구별없이 /RegexOpts.IngnoreCase 확인
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var words = new List<string>() { "dog", "Dog", "DOG", "Doggy" }; var rx = new Regex(@"\bdog\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); foreach (string word in words) { if (rx.IsMatch(word)) { Console.WriteLine($"{word} does match"); } else { Console.WriteLine($"{word} does not match"); } } |
1 2 3 4 5 6 |
dog does match Dog does match DOG does match Doggy does not match |
11. 장문에서 단어와 개수 찾기 LINK Select
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
var fileName = @"D:\bible.txt"; var text = File.ReadAllText(fileName); //MatchCollection matches = Regex.Matches(text, @"[a-z-A-Z']+"); MatchCollection matches = new Regex("[a-z-A-Z']+").Matches(text); //List<string> words = matches.Cast<Match>().Select(m => m.Value).ToList(); var words = matches.Cast<Match>().Select(m => m.Value).ToList(); var res = words .GroupBy(m => m) .OrderByDescending(g => g.Count()) .Select(x => new { word = x.Key, Count = x.Count() }) .Take(10); foreach (var r in res) { Console.WriteLine($"{r.word}: {r.Count}"); } |
1 2 3 4 5 6 7 8 9 10 11 12 |
the: 62103 and: 38848 of: 34478 to: 13400 And: 12846 that: 12576 in: 12331 shall: 9760 he: 9665 unto: 8942 |
12. Regex 로 Split 하기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Text.RegularExpressions; using System.Collections.Generic; ------------------------------------------------ string Text = "1 One, 2 Two, 3 Three is good."; string[] digits = Regex.Split(Text, @"\D+"); foreach (string value in digits) { int number; if (int.TryParse(value, out number)) { Console.WriteLine(value); } } |
1 2 3 4 5 |
1 2 3 |