regular expressions- creating links

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
dhnaves
Posts: 3
Joined: Fri Apr 04, 2003 6:07 pm
Contact:

regular expressions- creating links

Post by dhnaves »

hi,
i'm new to RE.

I'd like to know how to change various urls:

Code: Select all

http://www.yahoo.com 
http://www.whatever.com
http://www.ticketmaster.com
into a links like this: (using RE search and replace)
<a href="http://www.yahoo.com">http://www.yahoo.com</a>
<a href="http://www.whatever.com">http://www.whatever.com</a>
<a href="http://www.ticketmaster.com">http://www.ticketmaster.com</a>

thanks
User avatar
skaemper
Posts: 51
Joined: Mon Mar 03, 2003 1:07 pm
Location: (Northern) Germany
Contact:

Regular expressionist :)

Post by skaemper »

Hi,

first find an appropriate regex for the text-only-url

Code: Select all

http://www.whatever.com.
Note that I'm developing a regex that might fail to match a valid RE at one end of the spectrum, but may loose REs at the other end.
Your regex should be as simple as possible but yet do the work.
(Note: If you're going to match thousands of URLs you may better develop a really sophisticated RE.)
Anyway to get you started a kind of simplistic solution will do:

The first part is easy: Just match the literal text:

Code: Select all

http://
Then match the >>www.whatever.<< part. This is a repeating pattern:
Some letters and stuff followed by a "." followed by - itself.

Code: Select all

\([-a-zA-Z0-9_.]+\.\)+
should do that for you. Note the \( ... \) pair of escaped parenthesises to group the "stuff followed by a dot" pattern. The + forces to match at least one (partial) match.
Now, only the last part of the URL, >>com<<, is missing:

Code: Select all

[-a-zA-Z0-9_.]+
Let's glue it all together and put another pair of matching parens around it:

Code: Select all

\(http://\([-a-zA-Z0-9_.]+\.\)+[-a-zA-Z0-9_.]+\)
That's cool. 8)
That's what you want to find. And here's what you want to replace it for:

Code: Select all

<a href="\1">\1</a>
In the "Replace with" field you may refer to the matched RE with \1. And that's it.
BTW don't forget to check the "Regular Expression" check box in the "Condition" group of the "Replace" dialog.

Hope that helps.

PS.::idea: If you're going to work with regexes regularly you might like to read Jeffrey Friedls book "Mastering Regular Expressions" I mentioned in another thread just some moments ago.
"It's POLYMORPHIC!"
A former colleague
dhnaves
Posts: 3
Joined: Fri Apr 04, 2003 6:07 pm
Contact:

Post by dhnaves »

just after i sent the post, i was able to figure it out... with :

Code: Select all

(http://[a-z.a-z.a-z]+)
replace:

Code: Select all

<a href="\1">\1</a>
but yours was far better in terms of allowing different urls etc. man i can't believe i've never taken the time to learn this. i've spent so many hrs c/p things into files...

i'm reborn!

i really appreciate your help.

dave
// dave
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

You could also make a TextPad Macro to do this for you.

These are the steps to follow to make the macro:

A. Be sure the URL label (http://www.yahoo.com) is already on the document., and move the cursor to a line above the label.
B. Start Recording macro

1. Find next http:// (F5, Conditions=Text, Direction=Down, Find Next)
2. Close Find Window (Close)
3. Extend highlight to space (Shift-End)
4. Copy to clipboard (CTL-C)
5. Go to start of phrase (Home)
6. Type <a href-"
7. Go to end of phrase (End)
8. Type ">
9. Paste again (CTL-V)
10. Type </a>

C. Stop recording macro and save it.
D. Add the macro to your macro list using Configure, Preferences, Macros.
E. Now you can use the macro on a document by placing the cursor above the "http.." you want to process. Click on Macros, and select from the list.

Notes: This sample assumes one URL per line, nothing else on line. Should work OK on sample code you originally provided

If want to do on URL imbedded in document, more controls will be needed to get to beginning and end of URL label before doing Copy and starting typing at beginning of phrase. See Caution below re editing and "all conditions".


CAUTION! Macros cannot be edited, so it is important to be sure all your steps will be correct and work under all conditions. You will need to make separate identical macros to do one time, or do through the selected area, or do to the end of your document. It may be advisable to select 'run once" to give a little control.

But try the macro approach. When done correctly they are very effective......Good luck....
Hope this was helpful.............good luck,
Bob
dhnaves
Posts: 3
Joined: Fri Apr 04, 2003 6:07 pm
Contact:

how about this one..

Post by dhnaves »

thanks again for helping with the last post...
i am now getting frustrated because i don't know enough yet..

i want to transfrom names like this:

david_h_naves
john_smith
george_w_bush

into this:

d_naves
j_smith
g_bush

i've tried many things, but am stiil too much of a novice

thanks,
// dave
User avatar
skaemper
Posts: 51
Joined: Mon Mar 03, 2003 1:07 pm
Location: (Northern) Germany
Contact:

Post by skaemper »

dhnaves wrote:just after i sent the post, i was able to figure it out... with :

Code: Select all

(http://[a-z.a-z.a-z]+)
8< ... snip....
but yours was far better in terms of allowing different urls etc. man i can't believe i've never taken the time to learn this. i've spent so many hrs c/p things into files...
8< ...snip...
dave
Hi Dave,

note that
(http://[a-z.a-z.a-z]+)
won't do (what you might think it should...). With [...] you create a character class that matches one character (may be changed using '+', '?' and the like)out of the set of possibilitis. [a-z.]+ has exactly the same meaning (and effect) as the one given above.

Cheers,
Stephan
"It's POLYMORPHIC!"
A former colleague
Post Reply