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

    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Total regex noob... Is this possible?


    Here's my situation: I have code that looks like this:

    Code:
    previous: <A href="../Statutes/Title01/Chapter10/Section110.htm">Section 110</A>. Effect of Amendments to Statehood Act.<BR>
    next: <A href="../Statutes/Title02/Chapter05.htm">Chapter 5</A>. Alaska Air Commerce Act of 1960<BR>
    This little section is on about 40,000 pages, each "previous" and "next" link is going to be different on each page. What I'm trying to do is change all this code such that "previous/next", plus an arrow image, is INSIDE the link.

    So, will a regex let me say:
    "find 'previous:', and then find the next '>'"? This would allow me to replace the closing > of the link with what I actually want inside the link, without changing it for every link everywhere else.

    I've looked at the 'lookaround' options, but those seem like it will only match *directly* before or after a character.

    Can a regex 'skip' over parts of a line to find the match?

    Help me!
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,993
    Rep Power
    9397
    Altering only the bits you want isn't always the solution. In fact, sometimes it just doesn't work.

    How about you think of rearranging the line? Instead of
    Code:
    previous, <a>, inner text, </a>
    you get
    Code:
    <a>, previous, inner text, arrow, </a>
    It's not so much "moving the previous/next" as it is "moving everything".

    Code:
    Find     #(previous: )(<a[^>]+>)(.*?)</a>#i
    Replace  $2&larr; $1$3</a>
    
    Find     #(next: )(<a[^>]+>)(.*?)</a>#i
    Replace  $2$1 &rarr;$3</a>
    I don't know of a way you can conditionally add the left arrow or right arrow depending on whether it was a previous or next link. Thus two separate expressions. Maybe your language adds an extension to regular expressions to let you execute code as a replacement (like PHP's /e modifier).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0
    Yes, ultimately that is what I want to do. Have the line read:

    <a href="etc">[arrow img] previous: [current text]</a>

    But your solution would first 'move' the 'previous' and 'next' words inside the link, and THEN I could go through and replace (with garden variety find/replace) 'previous' with '<img>previous'?
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,993
    Rep Power
    9397
    Originally Posted by bsiariel
    But your solution would first 'move' the 'previous' and 'next' words inside the link, and THEN I could go through and replace (with garden variety find/replace) 'previous' with '<img>previous'?
    Not really: it moves everything around at the same time. One step.

    Well, one step for the next link, one step for the previous link.

    What language are you using? How familiar are you with regular expressions?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0
    Not familiar with regular expressions at all. I've only just started learning about them, so total noob.

    I'm not using any programming language, I'm trying to use the Find/Replace tool in Dreamweaver CS4, which supports regular expressions.

    The code you've provided above doesn't end up matching or replacing anything when I test it on gskinner . com / regexr

    I'll try dropping it in to Dreamweaver and see if something different happens.

    Edit: Nope, Dreamweaver can't find with the above expression either.

    Thanks for the handholding so far!
    Last edited by bsiariel; January 16th, 2009 at 07:33 PM. Reason: Update
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,993
    Rep Power
    9397
    Wouldn't be surprised if those #s were an issue.

    Just get the stuff in between them (drop that "i" at the end too) and look for a case-insensitive option.
    In the replacement string $X marks a capture pattern. Basically, anything in the search string that's enclosed by ()s gets "captured" for use later. The first set of parentheses are $1 in the replacement, then $2, $3, and so on.
    Dreamweaver might use a different symbol. I know some MS products use a backslash instead of a dollar sign (ie, \1, \2, \3, etc). If the replaced text looks odd then try that instead.

    Of course, reading any documentation there might be would greatly help too. Mostly syntax: the principles and stuff you can find elsewhere.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0
    Thanks! Removing the #s made it work!

    You saved me a ton of /headdesking.

IMN logo majestic logo threadwatch logo seochat tools logo