The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Total regex noob... Is this possible?
Discuss Total regex noob... Is this possible? in the Regex Programming forum on Dev Shed. Total regex noob... Is this possible? Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 15th, 2009, 06:32 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 4
Time spent in forums: 1 h 13 m 5 sec
Reputation 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! 
|

January 15th, 2009, 07:40 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
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← $1$3</a>
Find #(next: )(<a[^>]+>)(.*?)</a>#i
Replace $2$1 →$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).
|

January 15th, 2009, 07:43 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 4
Time spent in forums: 1 h 13 m 5 sec
Reputation 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'?
|

January 15th, 2009, 08:30 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Quote: | 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?
|

January 16th, 2009, 05:23 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 4
Time spent in forums: 1 h 13 m 5 sec
Reputation 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
|

January 16th, 2009, 07:41 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
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, 01:00 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 4
Time spent in forums: 1 h 13 m 5 sec
Reputation Power: 0
|
|
|
Thanks! Removing the #s made it work!
You saved me a ton of /headdesking.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|