Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Matching <sup> tags…

Last post 09-05-2008, 8:13 AM by Mark Bowen. 6 replies.
Sort Posts: Previous Next
  •  09-05-2008, 6:29 AM 46017

    Matching <sup> tags…

    Hi there,Well I went away after my last post and tried to learn as much Regex as I could but I now have a need for something else which at the moment really has me stumped.

    I would like to be able to match against some output from a database. People have entered either one of two things :

    ®

    or

    <sup>®</sup>

    I would like (if it's possible?) to be able to match just the version that only has the ® symbol so that I can replace it using PHP so that it becomes <sup>®</sup>. As different people are inputting the data I never know if they are going to be putting in one or the other but I can use PHP after the fact to turn one into the other and I need them all to end up as <sup>®</sup> so that is why I need to only match the single character entry.

    Hope that made sense? Wasn't really too sure of how to word that one.

    Thanks for any help on this one.

    Best wishes,

    Mark

     

  •  09-05-2008, 6:38 AM 46019 in reply to 46017

    Re: Matching <sup> tags…

    Hi Mark,

     You mean something like this?

    <?php
    $text = 'some text ® and some more text <sup>®</sup> ...';
    echo "$text\n";
    echo preg_replace('#(?<!<sup>)®(?!</sup>)#i', '<sup>®</sup>', $text);
    ?>

  •  09-05-2008, 7:00 AM 46020 in reply to 46019

    Re: Matching <sup> tags…

    Yep as far as I can tell that will do it!! :-)

    Brilliant, thank you very much. I don't suppose there is an easy way for me to understand exactly what is going on there is there?

    Thanks again for the help on this.

    Best wishes,

    Mark

     

  •  09-05-2008, 7:04 AM 46021 in reply to 46017

    Re: Matching <sup> tags…

    It may not apply to your source text, but in the event you want to make sure that character is surrounded by that tag regardless of other text within the same tag, here's that (adapted from a related solution):

    <?php
    $sourcestring="

    ®

    or

    <sup>®</sup>

    or

    <sup> this is a ® test </sup>";
    $sourcestring=preg_replace('~(?!(?:(?!</?sup>).)*</sup>)®~is','<sup>®</sup>',$sourcestring);
    echo $sourcestring;
    ?>




    looking for a new regex book?
    Regular Expressions Cookbook
  •  09-05-2008, 7:42 AM 46022 in reply to 46020

    Re: Matching <sup> tags…

    Mark Bowen:

    Yep as far as I can tell that will do it!! :-)

    Brilliant, thank you very much. I don't suppose there is an easy way for me to understand exactly what is going on there is there?

    Thanks again for the help on this.

    Best wishes,

    Mark

     

     

    You're welcome Mark. The (?<!...) and (?!... ) is called negative look-behind and negative look-ahead. You can read more about this in this article:

    http://www.regular-expressions.info/lookaround.html

     

  •  09-05-2008, 7:54 AM 46023 in reply to 46020

    Re: Matching <sup> tags…

    ----------------------------------------------------------------------
      (?<!                     look behind to see if there is not:
    ----------------------------------------------------------------------
        <sup>                    '<sup>'
    ----------------------------------------------------------------------
      )                        end of look-behind
    ----------------------------------------------------------------------
      ®                        '®'
    ----------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    ----------------------------------------------------------------------
        </sup>                   '</sup>'
    ----------------------------------------------------------------------
      )                        end of look-ahead
    ----------------------------------------------------------------------



    looking for a new regex book?
    Regular Expressions Cookbook
  •  09-05-2008, 8:13 AM 46024 in reply to 46023

    Re: Matching <sup> tags…

    Wow thanks for that. Should help me to understand it a lot better now.

    Thanks again for all the help on this one.

    Best wishes,

    Mark

     

View as RSS news feed in XML