First level matched
Second level matched
Third level matched
Fourth level matched
stuff
stuff
stuff
I\'m trying to match a block of div that have a particular id.. Here\'s my regex code:
]*\\s*id\\s*=\\s*[\"|\']content[\"|\']\\s*>[^/div]
DISCLAIMER: First, I agree that, in general, regex is not the best tool for parsing HTML. However, in the right hands, (and with a few caveats), Philip Hazel's powerful (and most assuredly non-REGULAR) PCRE library, (used by PHP's preg_*()
family of functions), does allow solving non-trivial data scraping problems such as this one (with some limitations and caveats - see below). The problem stated above is particularly complex to solve using regex alone, and regex solutions such as the one presented below are not for everyone and should never be attempted by a regex novice. To properly understand the answer below requires fairly deep comprehension of several advanced regex constructs and techniques.
Won't someone please think of the Children! Yes, I have read bobince's legendary answer and I know this is a touchy subject around here (to say the least). But please, if you are tempted to immediately click the down-vote arrow, because I am '/(?:actual|brave|stupid)ly/'
using the words: REGEX and: HTML in the same breath (and on a non-trivial problem no-less), I would humbly ask you to refrain long enough to read this entire post and to actually try this solution out for yourself.
With that in mind, if you would like to see how an advanced regex can be crafted to solve this problem, (for all but a few (unlikely) special cases - see below for examples), read on...
AN ADVANCED RECURSIVE REGEX SOLUTION: As Wes Hardaker correctly points out, DIV
s can (and frequently are) nested. However, he is not 100% correct when he says "you can't construct one that will match up until the correct
(?R)
, (?1)
, (?2)
, etc) which allow matching nested structures to any arbitrary depth (limited only by memory). For example, you can easily match balanced nested parentheses with this expression: '/\((?:[^()]++|(?R))*+\)/'
. Run this simple test if you have any doubts:
$text = 'zero(one(two)one(two(three)two)one)zero';
if (preg_match('/\((?:[^()]++|(?R))*+\)/', $text, $matches)) {
print_r($matches);
}
So if we can all agree that a PHP regex can, indeed, match nested structures, let's move on to the problem at hand. This particular problem is complicated by the fact that the outermost DIV
must have the id="content"
attribute, but any nested DIV
s may or may not. Thus, we can't use the (?R)
recursively-match-the-whole-expression construct, because the subexpression to match the outer DIV is not the same as the one needed to match the inner DIV
s. In this case, we need to have a capture group (in this case group 2), that will serve as a "recursive subroutine", which matches inner, nested DIV
's. So here is a tested PHP code snippet, sporting an advanced not-for-the-faint-of-heart-but-fully-commented-so-that-you-might-actually-be-able-to-make-some-sense-out-of-it regex, which correctly matches (in most cases - see below), a DIV
having id="content"
, which may itself contain nested DIV
s:
$re = '% # Match a DIV element having id="content".
]*? # Lazily match up to id attrib.
\bid\s*+=\s*+ # id attribute name and =
([\'"]?+) # $1: Optional quote delimiter.
\bcontent\b # specific ID to be matched.
(?(1)\1) # If open quote, match same closing quote
[^>]*+> # remaining outer DIV start tag.
( # $2: DIV contents. (may be called recursively!)
(?: # Non-capture group for DIV contents alternatives.
# DIV contents option 1: All non-DIV, non-comment stuff...
[^<]++ # One or more non-tag, non-comment characters.
# DIV contents option 2: Start of a non-DIV tag...
| < # Match a "<", but only if it
(?! # is not the beginning of either
/?div\b # a DIV start or end tag,
| !-- # or an HTML comment.
) # Ok, that < was not a DIV or comment.
# DIV contents Option 3: an HTML comment.
| # A non-SGML compliant HTML comment.
# DIV contents Option 4: a nested DIV element!
| ]*+> # Inner DIV element start tag.
(?2) # Recurse group 2 as a nested subroutine.
# Inner DIV element end tag.
)*+ # Zero or more of these contents alternatives.
) # End 2$: DIV contents.
# Outer DIV end tag.
%isx';
if (preg_match($re, $text, $matches)) {
printf("Match found:\n%s\n", $matches[0]);
}
As I said, this regex is quite complex, but rest assured, it does work! with the exception of some unlikely cases noted below - (and probably a few more that I would be very grateful if you could find). Try it out and see for yourself!
Should I use this? Would it be appropriate to use this regex solution in a production environment where hundreds or thousands of documents must be parsed with 100% reliability and accuracy? Of course not. Could it be useful for a limited one time run of some HTML files? (e.g. possibly the person who asked this question?) Possibly. It depends on how comfortable one is with advanced regexes. If the regex above looks like it was written in a foreign language (it is), and/or scares the dickens out of you, the answer is probably no.
It works? Yes. For example, given the following test data, the regex above correctly picks out the DIV
having the id="content"
(or id='content'
or id=content
for that matter):
Test Page
PCRE does recursion!
First level matched
Second level matched
Third level matched
Fourth level matched
stuff
stuff
stuff
stuff
stuff
CAVEATS: So what are some scenarios where this solution does not work? Well, DIV
start tags may NOT have any angle brackets in any of their attributes (it is possible to remove this limitation, but this adds quite a bit more to the code). And the following CDATA
spans, which contain the specific DIV
start tag we are looking for (highly unlikely), will cause the regex to fail:
stuff
in it ]]>
I would very much like to know of any others.
GO READ MRE3 As I said before, to truly grasp what is going on here requires a pretty deep understanding of several advanced techniques. These techniques are not obvious or intuitive. There is only one way that I know of to gain these skills and that is to sit down and study: Mastering Regular Expressions (3rd Edition) by Jeffrey Friedl (MRE3). (You will be glad you did!)
I can honestly say that this is the most useful book I have read in my entire life!
Cheers!
EDIT 2013-04-30 Fixed Regex. It previously disallowed a non-DIV
tag which immediately followed the DIV
start tag.