繁体   English   中英

正则表达式在满足条件的字符之间提取字符串

[英]Regex to extract string between characters satisfying a condition

我正在尝试提取{}之间的字符串,只要它们之间的字符串包含单词ltrch

输入字符串为:

{\\ 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}}}

我期望得到的输出是:

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

已尝试使用\\{\\s*(((?!\\{|\\}).)+)\\s*ltrch.*\\}(?<=\\{)([^{]+)ltrch.*(?=\\}) ,但是没有获得3场比赛。

我想,这样的事情:

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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM