January 15th, 2009, 07:32 PM
Total regex noob... Is this possible?
Here's my situation: I have code that looks like this:
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.
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>
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?
January 15th, 2009, 08:40 PM
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
previous, <a>, inner text, </a>
It's not so much "moving the previous/next" as it is "moving everything".
<a>, previous, inner text, arrow, </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).
Find #(previous: )(<a[^>]+>)(.*?)</a>#i
Replace $2← $1$3</a>
Find #(next: )(<a[^>]+>)(.*?)</a>#i
Replace $2$1 →$3</a>
January 15th, 2009, 08:43 PM
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'?
January 15th, 2009, 09:30 PM
Not really: it moves everything around at the same time. One step.
Originally Posted by bsiariel
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?
January 16th, 2009, 06:23 PM
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 08:33 PM.
January 16th, 2009, 08:41 PM
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.
January 20th, 2009, 02:00 PM
Thanks! Removing the #s made it work!
You saved me a ton of /headdesking.