Click here to Skip to main content
16,019,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
<samplefirst>

<samplesecond> 


I have given the pattern like this.

C#
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.
Posted
Updated 19-Jan-14 23:01pm
v2

1 solution

Use whitesapce instead of \r\n...

<samplefirst>(\s)*<samplesecond>


Sample code for C#...
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 ) );
 
Share this answer
 
v2
Comments
Member 10535273 20-Jan-14 5:33am    
Thanks for your replay. No other characters should come in between these two tags other than space or newline. so i cannot give the (.)*. Applying (\s)* alone not solving the problem as the text lies in two lines.
Kornfeld Eliyahu Peter 20-Jan-14 5:37am    
<samplefirst>(\s)*<samplesecond>
(http://www.w3schools.com/jsref/jsref_regexp_whitespace.asp)
Member 10535273 20-Jan-14 5:40am    
(\s)* not working.(\s)* works if i give the two tags in same line, but not for two lines.
Kornfeld Eliyahu Peter 20-Jan-14 5:42am    
Where you tested it? With me it works perfectly (as \s stands for all whitespace like ff, cr, tab...).
Member 10535273 20-Jan-14 6:00am    
yes...I tested. Its not working in c#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900