Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace RegexTest
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- internal class Program
- {
- private static void Main(string[] args)
- {
- const string File1 = "You are very beautiful, can you give me your number?";
- const string File2 = "Beautiful is Beyonce, not me.";
- const string FindWordsRegex = @"\b\w+\b";
- var GetWords = new Regex(FindWordsRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled);
- var file1Words = GetWords.Matches(File1).OfType<Match>().ToList();
- Console.WriteLine("file1Words:");
- file1Words.ForEach(w => Console.WriteLine(w));
- var file2Words = GetWords.Matches(File2).OfType<Match>().ToList();
- Console.WriteLine("file2Words:");
- file2Words.ForEach(w => Console.WriteLine(w));
- var words = new List<string>();
- file1Words.ForEach(a => file2Words.ForEach(b => { if (string.Compare(a.Value, b.Value, true) == 0) words.Add(a.Value); }));
- Console.WriteLine("words:");
- words.ForEach(w => Console.WriteLine(w));
- var ReplaceRegex = string.Format("(?={0})", words[0]);
- Console.WriteLine("Replace Regex == \"{0}\"", ReplaceRegex);
- var Search = new Regex(ReplaceRegex, RegexOptions.Compiled | RegexOptions.IgnoreCase);
- Console.WriteLine("Replace Target: {0}", words[0]);
- var r2 = Search.Matches(File1);
- var result = Search.Replace(File1, File2);
- Console.WriteLine("Final Result: {0}\r\n", result);
- Console.WriteLine("Press Enter To Quit");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement