#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Rookie: Partial Replacement


    Dear all,

    I tried out a lot of things now and didn't get it to work.
    What I want to achieve is

    Source:
    <A HREF="#pgfId-697091" CLASS="footnote">1</A>

    Target:
    <SUP>1</SUP>

    I do have other links in my source which have to stay unchanged.
    I managed to get something like this to work, but it selects the 1 as well and I just don't know on how to call the replacement.

    <A HREF="#[^>]*>(.*?)</A>

    Can someone please help me?

    Thanks in advance
    Alex
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Be as specific as possible.
    Code:
    <A HREF="#pgfId-\d+" CLASS="footnote">(\d+)</A>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0
    Originally Posted by requinix
    Be as specific as possible.
    Code:
    <A HREF="#pgfId-\d+" CLASS="footnote">(\d+)</A>
    ok, thanks for the answer!
    but http://regexpal.com/ shows me that this pattern would still match (and therefore?) replace the 1 as well.
    Is that true?

    Thanks in advance!

    Alexander Odenthal
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Originally Posted by Alexander Odent
    but http://regexpal.com/ shows me that this pattern would still match (and therefore?) replace the 1 as well.
    Is that true?
    If you don't use the replacement string to put it back, yes.
  8. #5
  9. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93

    Code alignment


    Alex,

    requinix is right, but just to flesh out the answer. You need two expressions, one for the match, one for the replace.

    For educational purposes, here is another candidate for the match:
    Code:
    (?i)<a href="#pgfid[^>]*?>([^<]*?)</a
    Here is a candidate for the replace:
    Code:
    <sup>\1<sup>
    Bringing it all together in PHP, you would write something like:
    PHP Code:
    $subject='the string you are trying to test';
    $result preg_replace('%(?i)<a href="#pgfid[^>]*?>([^<]*?)</a%m''<sup>\1<sup>'$subject); 
    Let me know if you need any more details.

IMN logo majestic logo threadwatch logo seochat tools logo