2

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>
    </content_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?

Andrei Serdeliuc ॐ
  • 5,828
  • 5
  • 39
  • 66

5 Answers5

6

… 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.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • i was about to answer the same. wondering how many times this has been asked and answered before. and if these kind of questions should be marked duplicate. – ax. Nov 07 '09 at 12:13
  • Thanks. I've been so concentrated on getting it to work that I've completely overseen the fact that it might just not work. – Andrei Serdeliuc ॐ Nov 07 '09 at 12:13
  • Depending on what language/regex-flavour you are using there you might be able to hack it with ‘recursive expressions’. But really, indeed, regex it totally the wrong tool for parsing HTML. – bobince Nov 07 '09 at 14:12
  • @bobince: true. But this still assumes valid XML which is a bit of a stretch for most HTML, unfortunately. For HTML, the only way to go is really to transform the document into some kind of DOM and then operating on it. – Konrad Rudolph Nov 07 '09 at 15:06
3

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

Saurabh
  • 445
  • 6
  • 19
1

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.

Paul Butcher
  • 6,902
  • 28
  • 39
0

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>

)
jk2K
  • 4,279
  • 3
  • 37
  • 40
-2

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.

Community
  • 1
  • 1
Maxim Suslov
  • 4,335
  • 1
  • 35
  • 29
  • 1
    Breaks if there is an additional space between content_block and id, an attribute between content_block and id, or an attribute after id. Hangs if you add (only < is required to be encoded in attribute values). The list undoubtedly goes on. Always use an XML parsing library. They are easy to use and don't require you to work around the myriad issues you will run into trying to parse it by hand with a regex. – Chas. Owens Feb 09 '15 at 14:26