Matching pair tag with regex
I'm trying to retrieve specific tags with their content out of an xhtml document, but it's matching the wrong ending tags.
In the following content:
<cache_namespace name="content">
<content_block id="15">
some content here
<cache_namespace name="user">
<content_block id="welcome">
Welcome Apikot!
</content_block>
</cache_namespace>
</c开发者_StackOverflow社区ontent_block>
</cache_namespace>
The content_block ending tag for id="welcome" actually get's matched as the ending tag of the first opening content_block tag.
The regex I'm using is:
/<content_block id="(.*)">([\w\W]*?)<\/content_block>/i
Any pointers as to where I'm failing?
… and the answer is always the same: HTML + regex cannot be done. Sorry. Use an HTML parsing library for your particular framework. Or, if your document is guaranteed to only contain valid XHTML, take the XPath approach as proposed by jitter in a comment.
this might help i found tutorial on http://www.regular-expressions.info/examples.html which mentions capturing pair of string recurring in given text. suggestion is to use ? after .* to make it stop after first occurrence of ending string of the pair in text
This is a known problem with regex - you can't match pairs. Matching is either greedy, in which it matches the last one it finds, or non-greedy, in which it matches the first. You can't persuade a regex to count opening and closing brackets.
I would recommend loading it into a DOM and using that. If you are trying to implement an HTML parser, I would recommend using regex to lex it, then a left-right parser to parse the output of your lexer.
thanks to @Jan Żankowski and @ikegami, their answer gave me inpiration
Let me use PHP to demonstrate the code
<?php
$xml = <<<EOT
<cache_namespace name="content">
<content_block id="15">
some content here
<cache_namespace name="user">
<content_block id="welcome">
Welcome Apikot!
</content_block>
</cache_namespace>
</content_block>
</cache_namespace>
EOT;
preg_match('/<cache_namespace[^>]+>((?:(?!(<\/?div>)).)*)<\/cache_namespace>/s', $xml, $matches);
print_r($matches);
regex note
s
option: a.
in the pattern matches all characters, including newlines- The key here is that
(?:(?!STRING).)*
is to strings as[^CHAR]*
is to characters
result
Array
(
[0] => <cache_namespace name="content">
<content_block id="15">
some content here
<cache_namespace name="user">
<content_block id="welcome">
Welcome Apikot!
</content_block>
</cache_namespace>
</content_block>
</cache_namespace>
[1] =>
<content_block id="15">
some content here
<cache_namespace name="user">
<content_block id="welcome">
Welcome Apikot!
</content_block>
</cache_namespace>
</content_block>
)
Parsing XHTML or XML isn't hard. I assumed that you have valid or well-formed code.
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
my $xml = <<"EOF";
<cache_namespace name="content">
<content_block id="15">
some content here
<cache_namespace name="user">
<content_block id="welcome">
Welcome Apikot!
</content_block>
</cache_namespace>
</content_block>
</cache_namespace>
EOF
while ($xml =~ m!
<(content_block)\sid="welcome"> # Start tag definition.
(\s* # It may consists of
(?: <\!--.*?--> # - comment
| [^<]* # - text
| <[^>]+/> # - another closed tag
| <\s*(\w+)[^>]*> # - another tag with some content
(?2)+ # (recursive definition of possible tag content)
</\3>
)
)*
</\1>
!sxgc) {
print "==> $&\n\n";
}
Please modify start tag definition for another content (like <\s*(\w+)[^>]*+>
). Anyway it's a good start point.
If you will not use recursion (line with (?2)+
) you will stuck on such examples. This code can handle them all (please look here before) or can easily adapt for new situations.
精彩评论