Apr
1
Written by:
Steve Gray
4/1/2011 9:52 AM
The most straightforward way to match something, but only if it occurs within something else, is to use two regular expressions.
Use the first regex to match the blocks of text that contain what you're looking for. Use the second regex to search within the text matched by the first regex to find what you're looking for.
Suppose you want to match <p> tags and their contents, but only if they occur within a <div> tag. Other <p> tags should be skipped. You can easily match the <div> tags and their contents with
<div>.*?</div>
You can just as easily match the <p> tags with
<p>.*?</p>
If you restrict the second regex to text matched by the first, you'll get all the <p> tags that are inside a <div> tag, and none of the other <p> tags the file may contain.
As always, I welcome your comments!