Jump to content

Archived

This topic is now archived and is closed to further replies.

johnnyk

Regex

Recommended Posts

Why doesn't
/<OL><LI>.[^<\/LI>]*<\/LI>/s

match

<OL><LI><OL TYPE="a"><LI TYPE="a">text.</LI>

Wouldn't . include <OL TYPE="a"><LI TYPE="a">

Also, if I wanted to make it single line and case insensitive, what would I do? Would it just be /regex/si?

Share this post


Link to post
Share on other sites
It won't match that because of the negated characters. Something like this:

[code]/<OL><LI>(.*?)<\/LI>/s[/code]

Would make more sense. For case insensitive matching, use the PCRE_CASELESS modifier (i). For single line only, remove the PCRE_DOTALL modifier (s).

[code]/<OL><LI>(.*?)<\/LI>/i[/code]

Share this post


Link to post
Share on other sites
So what exactly was wrong with my regex. I thought it said "match any characters (except for a bullet end), until you find a bullet end."

And what if I wanted it to not stop at \n (/regex/s) AND be case insensitive (/regex/i)? How would I combine them?

Share this post


Link to post
Share on other sites
It says "match any characters except <, /, L, I and >". As it is in a character class, each character is considered independent.

Simply use /pattern/si
It doesn't matter the order (so it could be /pattern/is); everything outside the delimiter is considered a pattern modifier.

Share this post


Link to post
Share on other sites
More regex - why isn't
/<DL><DD>(.*?)<\/DD>/si
matching
<I>text.</I> [b]<DL><DD>text.</DD>[/b]</DL><HR ALIGN="LEFT" .....

Share this post


Link to post
Share on other sites
It does.

[code]
<?php
    $string = '<I>text.</I> <DL><DD>text.</DD></DL><HR ALIGN="LEFT"';
    if (preg_match('/<DL><DD>(.*?)<\/DD>/si', $string)) {
        print 'matched';
    }
?>
[/code]

Share this post


Link to post
Share on other sites
Oh man my fault. I just realized I used strip_tags() to strip everything but <ol> and <li>. Thanks alot.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.