1
votes

I am trying to extract the string between { and } provided the string in between contains the word ltrch

Input string is:

{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ltrch A }{\b\ltrch DD}\li0\ri0\sa0\sb0\fi0\ql\par} {\f2 {\b\i\ul\ltrch Italuic}\li0\ri0\sa0\sb0\fi0\ql\par} } }

The output I am expecting to get is:

{\ltrch A }, {\b\ltrch DD}, {\b\i\ul\ltrch Italuic}

Have been trying around with \{\s*(((?!\{|\}).)+)\s*ltrch.*\} and (?<=\{)([^{]+)ltrch.*(?=\}), however am not getting 3 matches.

1

1 Answers

0
votes

I guess, something like this:

String source = @"{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ltrch A }{\b\ltrch DD}\li0\ri0\sa0\sb0\fi0\ql\par}
{\f2 {\b\i\ul\ltrch Italuic}\li0\ri0\sa0\sb0\fi0\ql\par}
    }
  }";

// start with {
// followed by any number of any characters with { and } excluded
// ltrch 
// followed by any number of any characters with { and } excluded
// end with }
String pattern = @"\{[^{}]*ltrch[^{}]*\}";

var result = Regex.Matches(source, pattern)
  .OfType<Match>()
  .Select(match => match.Value);

 // Test:    
 // {\ltrch A }, {\b\ltrch DD}, {\b\i\ul\ltrch Italuic}
 Console.Write(String.Join(", ", result));