正则表达式匹配两行中的文本不起作用 [英] Regex match for text in two lines not working

查看:116
本文介绍了正则表达式匹配两行中的文本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想写一个模式来查找跨越两行的文本匹配。



我想解释一下这个场景。在给定页面中,我想查找以下是否存在。



< samplefirst> 

< samplesecond>





我已经给出了这样的模式。



正则表达式匹配= 正则表达式( @ < samplefirst>(\r?\ n?| \ n | \ s?)*< samplesecond>,RegexOptions.Multiline)





我尝试了很多\\\
和\r的组合。但是,如果两个标签分为两行,那么就没有任何结果。我检查了一些正则表达式验证网站,如regexpal.com。那个模式在那里很好。但它在Visual Studio中找不到任何匹配。



最早的任何帮助都是值得的。

提前致谢。

解决方案

使用whitesapce代替\\\\ nn ...



< samplefirst>(\ s)*< samplesecond> 





C#的示例代码...

正则表达式oRegex = 正则表达式( @ < samplefirst>(\ s)*< samplesecond>); 

MatchCollection oMatchCollection = oRegex .Matches( string .Format( < samplefirst> {0} {0}< samplesecond>< samplefirst> {0} no {0}< samplesecond>,Environment.NewLine));


Hi,

I want to write a pattern to find a match for text that spans across two lines.

I would like to explain the scenario. In a given page, I want to find whether the following present or not.

<samplefirst>

<samplesecond> 



I have given the pattern like this.

Regex match = new Regex(@"<samplefirst>(\r?\n?|\n|\s?)*<samplesecond>", RegexOptions.Multiline)



I have tried out many combinations of \n and \r. But nothing make it out if the two tags are in two lines. I checked with some regex validation sites like "regexpal.com". The pattern is woking fine over there. But it fails to find any match in Visual studio.

Any help at the earliest is appreciable.
Thanks in advance.

解决方案

Use whitesapce instead of \r\n...

<samplefirst>(\s)*<samplesecond>



Sample code for C#...

Regex oRegex = new Regex( @"<samplefirst>(\s)*<samplesecond>" );

MatchCollection oMatchCollection = oRegex .Matches( string.Format( "<samplefirst>{0} {0}<samplesecond><samplefirst>{0}no{0}<samplesecond>", Environment.NewLine ) );


这篇关于正则表达式匹配两行中的文本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆