Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regex help - matching nested tags without matching adjacent ones

    Hi. I'm in need of a regular expression that will match nested <abbr> tags, then delete any which are children of other abbr tags. For example, if my <abbr>ising algorithm turns up this: <abbr>X<abbr>HTML</abbr></abbr>, I want this regex to match that and to turn out <abbr>XHTML</abbr>.

    This is how far I've got:
    PHP Code:
    $text preg_replace(
        
    '/<abbr title="(.*)">(.*)<abbr.*>(.*)<\/abbr><\/abbr>/',
        
    '<abbr title="$1">$2$4</aabr>',
        
    $text); 
    However, this matches things like "<abbr>XHTML</abbr> and <abbr>XML</abbr>", and shortens them when it shouldn't.

    Any help is appreciated.

    PS, perl format regex is preferred, but not necessary.
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #2
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Try adding some lazy operators...

    Code:
     $text = preg_replace(
        '/<abbr title="(.*?)">(.*?)<abbr.*?>(.*?)<\/abbr><\/abbr>/',
        '<abbr title="$1">$2$4</aabr>',
        $text);

  • #3
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That looks like it's working... thanks very much!!
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bcarl314
    Try adding some lazy operators...
    <answer mode="nitpick">
    The code in your answer is absolutely correct, but regarding the question marks... I've never seen them refered to as lazy operators. Usually these thingies are called "greedy quantifiers". For me, lazy operators would suggest lazy evaluation of expressions, like in "if (funcA() || funcB())", where the condition fails if funcA() returns false, and never calls upon funcB().

    Here's some reading material about greedy quantifiers. Be aware that this feature is only available for perl-like regexes.
    </answer>
    De gustibus non est disputandum.

  • #5
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Sorry, I was too ? to write it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •