Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression for text between tags 2

Status
Not open for further replies.

RyanEK

Programmer
Apr 30, 2001
323
AU
Hi,

Let's say I have the following text...

Code:
<div class="info">
<h5>Test1</h5>
<a href="/19369/">Test1a</a><br/>
</div>

<div class="info">
<h5>Test2</h5>
<a href="/19361/">Test2a</a><br/>
</div>

<div class="info">
<h5>Test3</h5>
<a href="/19360/">Test3a</a><br/>
</div>

What regex pattern can I use to grab a collection of texts between the <div class="info"> and </div> tags? ie. 3 occurrences?

Any help would be appreciated. A star for any helpful answer :) Thanks.

Ryan

 
thread855-1491831

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hmmm still can't seem to get the right pattern. From my example above, how would I return this match (1 of 3)?

Code:
<h5>Test1</h5>
<a href="/19369/">Test1a</a><br/>

Thanks
 
<div class="info">(.*?)</div>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Unfortunately there is a degree of freedom that innerHTML can have is the line-break/tab whitespaces. Hence, .*? is defective in capturing them in that generality. A restricted scope pattern (still with many assumptions) in the same vein would be like this.
[tt] string pattern=@"<div class=""info"">[\s\S]*?</div>";[/tt]

An improvement of directly getting the submatches would be to make good use of the .net added supported of look-behind, like this.
[tt] string pattern=@"(?<=<div class=""info"">)[\s\S]*?(?=</div>)";[/tt]
 
the overloaded Regex.Matches(input, pattern, regexoption) may also help in this scenario.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Tsuji, that's exactly what I was looking for :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top