Advertisement
Guest User

DocumentOccurrencesFinder

a guest
Feb 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows.Documents;
  8.  
  9. namespace RegExp
  10. {
  11.     class DocumentOccurrencesFinder
  12.     {
  13.         public IEnumerable<TextRange> GetOccurrencesRanges(FlowDocument flowDocument, Regex regex)
  14.         {
  15.             if (regex != null)
  16.             {
  17.                 List<TextRange> result = new List<TextRange>();
  18.  
  19.                 {
  20.                     string text = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd).Text;
  21.                     string pattern = regex.ToString();
  22.  
  23.                     if (CheckIfMatchesWholeString(text: text, pattern: pattern))
  24.                     {
  25.                         result.Add(new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd));
  26.                         return result;
  27.                     }
  28.  
  29.                 }
  30.  
  31.                 TextPointer pointer = flowDocument.ContentStart;
  32.  
  33.                 while (pointer != null)
  34.                 {
  35.                     if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
  36.                     {
  37.                         string textRun = pointer.GetTextInRun(LogicalDirection.Forward);
  38.                         MatchCollection matches = regex.Matches(textRun);
  39.                         foreach (Match match in matches)
  40.                         {
  41.                             int startIndex = match.Index;
  42.                             int length = match.Length;
  43.                             TextPointer start = pointer.GetPositionAtOffset(startIndex);
  44.                             TextPointer end = start?.GetPositionAtOffset(length);
  45.  
  46.                             TextRange textRange = new TextRange(start, end);
  47.  
  48.                             result.Add(textRange);
  49.                         }
  50.                     }
  51.      
  52.                     pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
  53.                 }
  54.  
  55.                 return result;
  56.             }
  57.  
  58.             return null;
  59.         }
  60.         private bool CheckIfMatchesWholeString(string text, string pattern)
  61.         {
  62.             return pattern.EndsWith("$") &&
  63.                    pattern.StartsWith("^") &&
  64.                    pattern.Trim('^', '$') == text.RemoveAll('\r', '\n') ||
  65.                    pattern == text.RemoveAll('\r', '\n');
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement