C# 正则表达式匹配多行文本块?
我有一个文本文件,其中包含200多条以下格式的记录:C# 正则表达式匹配多行文本块?,c#,regex,C#,Regex,我有一个文本文件,其中包含200多条以下格式的记录: @INPROCEEDINGS{Rajan-Sullivan03, author = {Hridesh Rajan and Kevin J. Sullivan}, title = {{{Eos}: Instance-Level Aspects for Integrated System Design}}, booktitle = {ESEC/FSE 2003}, year = {2003}, pages = {297--3
@INPROCEEDINGS{Rajan-Sullivan03,
author = {Hridesh Rajan and Kevin J. Sullivan},
title = {{{Eos}: Instance-Level Aspects for Integrated System Design}},
booktitle = {ESEC/FSE 2003},
year = {2003},
pages = {297--306},
month = sep,
isbn = {1-58113-743-5},
location = {Helsinki, FN},
owner = {Administrator},
timestamp = {2009.03.08}
}
@INPROCEEDINGS{ras-mor-models-06,
author = {Awais Rashid and Ana Moreira},
title = {Domain Models Are {NOT} Aspect Free},
booktitle = {MoDELS},
year = {2006},
editor = {Oscar Nierstrasz and Jon Whittle and David Harel and Gianna Reggio},
volume = {4199},
series = {Lecture Notes in Computer Science},
pages = {155--169},
publisher = {Springer},
bibdate = {2006-12-07},
bibsource = {DBLP, http://dblp.uni-trier.de/db/conf/models/models2006.html#RashidM06},
isbn = {3-540-45772-0},
owner = {aljasser},
timestamp = {2008.09.16},
url = {http://dx.doi.org/10.1007/11880240_12}
}
基本上a记录以@开头,以a}结尾,所以我尝试以@开头,以}\n}结尾,但没有成功,它只会匹配第一条记录和另一条记录,因为它后面没有新行
string pattern = @"(^@)([\s\S]*)(}$\n}(\n))";
当我试图通过制造它来修复它时,它就像一根火柴一样匹配所有的东西
string pattern = @"(^@)([\s\S]*)(}$\n}(\n*))";
我一直在尝试,直到我达到以下模式,但它不起作用,请如果你可以修复它,或者可能给一个更有效的一个加上一点解释它的完成
这是我的密码:
string pattern = @"(^@)([\s\S]*)(}$\n}(\n))";
Regex regex = new Regex(pattern,RegexOptions.Multiline);
var matches = regex.Matches(bibFileContent).Cast<Match>().Select(m => m.Value).ToList();
string模式=@“(^@)([\s\s]*)(}$\n}(\n))”;
Regex Regex=新的Regex(模式,RegexOptions.Multiline);
var matches=regex.matches(bibFileContent.Cast().Select(m=>m.Value.ToList();
我认为问题在于您的输入没有在\n之前完成,因此您的第二条记录不匹配。你应该给我一个替代品$
这将在第1组中获得以下记录:
@(.*?)^}(?:[\r\n]+|$)
请注意,您必须使用m
和s
修饰符
使用此代码:
Regex regex = new Regex(pattern, RegexOptions.Multiline | RegexOptions.Singleline);
MatchCollection mc = regex.Matches(bibFileContent);
List<String> results = new List<String>();
foreach (Group m in mc[0].Groups)
{
results.Add(m.Value);
}
Regex Regex=new Regex(模式,RegexOptions.Multiline | RegexOptions.Singleline);
MatchCollection mc=regex.Matches(bibFileContent);
列表结果=新列表();
foreach(mc[0]中的m组。组)
{
结果:增加(m值);
}
我认为问题在于您的输入没有在\n之前完成,因此您的第二条记录不匹配。你应该给我一个替代品$
这将在第1组中获得以下记录:
@(.*?)^}(?:[\r\n]+|$)
请注意,您必须使用m
和s
修饰符
使用此代码:
Regex regex = new Regex(pattern, RegexOptions.Multiline | RegexOptions.Singleline);
MatchCollection mc = regex.Matches(bibFileContent);
List<String> results = new List<String>();
foreach (Group m in mc[0].Groups)
{
results.Add(m.Value);
}
Regex Regex=new Regex(模式,RegexOptions.Multiline | RegexOptions.Singleline);
MatchCollection mc=regex.Matches(bibFileContent);
列表结果=新列表();
foreach(mc[0]中的m组。组)
{
结果:增加(m值);
}
如果使用Matches方法,则需要这种处理平衡花括号的图案:
string pattern = @"@[A-Z]+{(?>[^{}]+|(?<open>{)|(?<-open>}))*(?(open)(?!))}";
Regex regex = new Regex(pattern);
如果chuncks可以包含@
字符,则可以通过更具描述性的前瞻性使其更加健壮:
string[] result = Regex.Split(input, @"[^}]*(?=@[A-Z]+{)");
或
如果使用Matches方法,则需要这种处理平衡花括号的图案:
string pattern = @"@[A-Z]+{(?>[^{}]+|(?<open>{)|(?<-open>}))*(?(open)(?!))}";
Regex regex = new Regex(pattern);
如果chuncks可以包含@
字符,则可以通过更具描述性的前瞻性使其更加健壮:
string[] result = Regex.Split(input, @"[^}]*(?=@[A-Z]+{)");
或
您可以使用如下简单的正则表达式:
(@[^@]+)
这样做的目的是匹配以@开头但没有另一个@的内容。顺便说一句,如果您只是想匹配模式而不是捕获模式,只需删除capturin组:
@[^@]+
您可以使用如下简单的正则表达式:
(@[^@]+)
这样做的目的是匹配以@开头但没有另一个@的内容。顺便说一句,如果您只是想匹配模式而不是捕获模式,只需删除capturin组:
@[^@]+
这看起来像是一个平衡群体的候选人
# @"(?m)^[^\S\r\n]*@[^{}]+(?:\{(?>[^{}]+|\{(?<Depth>)|\}(?<-Depth>))*(?(Depth)(?!))\})"
(?m)
^ [^\S\r\n]*
@ [^{}]+
(?:
\{ # Match opening {
(?> # Then either match (possessively):
[^{}]+ # Anything (but only if we're not at the start of { or } )
| # or
\{ # { (and increase the braces counter)
(?<Depth> )
| # or
\} # } (and decrease the braces counter).
(?<-Depth> )
)* # Repeat as needed.
(?(Depth) # Assert that the braces counter is at zero.
(?!) # Fail if it isn't
)
\} # Then match a closing }.
)
这看起来像是一个平衡群体的候选人
# @"(?m)^[^\S\r\n]*@[^{}]+(?:\{(?>[^{}]+|\{(?<Depth>)|\}(?<-Depth>))*(?(Depth)(?!))\})"
(?m)
^ [^\S\r\n]*
@ [^{}]+
(?:
\{ # Match opening {
(?> # Then either match (possessively):
[^{}]+ # Anything (but only if we're not at the start of { or } )
| # or
\{ # { (and increase the braces counter)
(?<Depth> )
| # or
\} # } (and decrease the braces counter).
(?<-Depth> )
)* # Repeat as needed.
(?(Depth) # Assert that the braces counter is at zero.
(?!) # Fail if it isn't
)
\} # Then match a closing }.
)
更具体地说明“不工作”的含义。举例说明你想要的输出。它只匹配第一条记录。这不是更简单吗?字符串模式=@“@([^;]*)}”;这是一个玩正则表达式的好地方,我也建议使用组。例如,请更具体地说明“不工作”的含义。举例说明你想要的输出。它只匹配第一条记录。这不是更简单吗?字符串模式=@“@([^;]*)}”;这是一个玩正则表达式的好地方,我也建议使用组。例如,请参见您的正则表达式正在工作,但在我尝试它时,它对代码不起作用,我使用了MultileOptions您必须使用Multiline和SingleLine选项我尝试过此RegeOptions选项=RegeOptions.Multiline | RegeOptions.SingleLine;但是仍然没有匹配我编辑了答案,我包括了我测试的匹配代码仍然不起作用,我已经用你的模式粘贴了整个文档内容你的正则表达式起作用了,但是当我尝试它时,它对代码不起作用,我使用了Multiline选项您必须使用Multiline和SingleLine选项我尝试过此RegexOptions选项=RegexOptions.Multiline | RegexOptions.SingleLine;但是仍然没有匹配我编辑了答案,我包括了我测试的匹配代码仍然不起作用,我已经用你的模式粘贴了整个文档内容,上面的效果很好,但是当我尝试时,我得到了更多的结果,因为似乎有一些记录中有“@”,我得到了283/243,上面的效果很好,但当我尝试时,我得到了更多的结果,因为似乎有些记录中有“@”,我得到了283/243