fork download
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. String word = @"hello";
  9. String input = @"hello. I am a string. hello friend. Many hellos. hello .hello";
  10.  
  11. Console.WriteLine("--- Input ---" + Environment.NewLine + input + Environment.NewLine);
  12. Console.WriteLine("--- Matches ---");
  13.  
  14.  
  15. Regex regex = new Regex(@"(?<=\s)\b" + word + @"\b|\b" + word + @"\b(?=[\s\.])");
  16. MatchCollection mc = regex.Matches(input);
  17. foreach(Match m in mc)
  18. {
  19. Console.WriteLine(m.Value + " - " + m.Index.ToString());
  20. }
  21. }
  22. }
Success #stdin #stdout 0.11s 24608KB
stdin
Standard input is empty
stdout
--- Input ---
hello. I am a string. hello friend. Many hellos. hello .hello

--- Matches ---
hello - 0
hello - 22
hello - 49