【问题标题】:Regex to extract string between characters satisfying a condition正则表达式在满足条件的字符之间提取字符串
【发布时间】:2016-03-17 06:56:55
【问题描述】:
我正在尝试提取{ 和} 之间的字符串,前提是两者之间的字符串包含单词ltrch
输入字符串是:
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0
时代新罗马;}{\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 斜体}\li0\ri0\sa0\sb0\fi0\ql\par} } }
我期望得到的输出是:
{\ltrch A }、{\b\ltrch DD}、{\b\i\ul\ltrch Italuic}
一直在尝试
\{\s*(((?!\{|\}).)+)\s*ltrch.*\}
和(?<=\{)([^{]+)ltrch.*(?=\}),但是没有得到 3 场比赛。
【问题讨论】:
标签:
c#
.net
regex
pattern-matching
【解决方案1】:
我猜是这样的:
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));