Click here to Skip to main content
15,176,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

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.



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 "". 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.
Updated 20-Jan-14 0:01am

1 solution

Use whitesapce instead of \r\n...


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 ) );
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
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#.
Kornfeld Eliyahu Peter 20-Jan-14 6:24am
updated the answer with c# (that works with me)...
Member 10535273 22-Jan-14 6:53am
Thank you for helping out to solve the issue...I was reading the file line by line inside a loop. So which ever pattern I was giving, it never checked for multiple line. Now I read the file to a string outside the loop n then checked for the match.(\s)* works perfectly then...(\s) considers \s,\n etc..Once again thank you for providing the help.
Kornfeld Eliyahu Peter 22-Jan-14 6:58am
Glad to help...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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