Advertisement
RoboJ1M

Regex Class Answer for Stack Overflow

Nov 27th, 2018
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. namespace RegexTest
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text.RegularExpressions;
  7.     internal class Program
  8.     {
  9.         private static void Main(string[] args)
  10.         {
  11.             const string File1 = "You are very beautiful, can you give me your number?";
  12.             const string File2 = "Beautiful is Beyonce, not me.";
  13.             const string FindWordsRegex = @"\b\w+\b";
  14.             var GetWords = new Regex(FindWordsRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled);
  15.             var file1Words = GetWords.Matches(File1).OfType<Match>().ToList();
  16.             Console.WriteLine("file1Words:");
  17.             file1Words.ForEach(w => Console.WriteLine(w));
  18.             var file2Words = GetWords.Matches(File2).OfType<Match>().ToList();
  19.             Console.WriteLine("file2Words:");
  20.             file2Words.ForEach(w => Console.WriteLine(w));
  21.             var words = new List<string>();
  22.             file1Words.ForEach(a => file2Words.ForEach(b => { if (string.Compare(a.Value, b.Value, true) == 0)  words.Add(a.Value); }));
  23.             Console.WriteLine("words:");
  24.             words.ForEach(w => Console.WriteLine(w));
  25.             var ReplaceRegex = string.Format("(?={0})", words[0]);
  26.             Console.WriteLine("Replace Regex == \"{0}\"", ReplaceRegex);
  27.             var Search = new Regex(ReplaceRegex, RegexOptions.Compiled | RegexOptions.IgnoreCase);
  28.             Console.WriteLine("Replace Target: {0}", words[0]);
  29.             var r2 = Search.Matches(File1);
  30.             var result = Search.Replace(File1, File2);
  31.             Console.WriteLine("Final Result: {0}\r\n", result);
  32.             Console.WriteLine("Press Enter To Quit");
  33.             Console.ReadLine();
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement