add target equals blank to links without any target

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
whoisbambam
Posts: 3
Joined: Tue Oct 28, 2008 8:23 pm

add target equals blank to links without any target

Post by whoisbambam »

I found this online:
('/<a[^>]*?href=[\'"](.*?)[\'"][^>]*?>(.*?)<\/a>/si','<a href="$1" target="_blank">$2</a>',$s);

first, i'd just like to see that example for textpad, without the exclusions below so it is easier for me to follow.

But really this is what I need:

I need to open an html document, and add a space and target="_blank" to the end of the anchor in all cases except when within that opening anchor there is already the word 'target' and, if possible, in all cases except when there is not a match for 'searchengine' anywhere within that anchor (ie i need to ignore http://www.searchengine-resources.com and NOT add target to it since that would be the domain name of the site).

Yes, sometimes the anchor is on two different lines. I imagine this may cause some issues, but even if i could partially accomplish this task it would make my life easier.

thanks for any help.

I truly have no idea how you all can understand these regular expressions.

If it is super simple I can find a simple pattern, but when it comes to quotes, slashes, variable character types, and especially backreferencing, I am lost.

do appreciate any time anybody can provide for even a partial soluton.

steven
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

The regular expression recogniser used by TextPad is very weak compared with modern tools. Therefore, many of the examples you find on the web will not work in TextPad. In particular, the non-greedy repetition operator (*?) is not available in TextPad. Using the greedy one (*) instead, the nearest equivalent in TextPadto your replacement is
Find what: <a[^>]*href=['"](.*)['"][^>]*>(.*)</a>
Replace with: <a href="\1" target="_blank">\2</a>

[X] Regular expression

Replace All
This assumes you are using Posix regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
However, this regex replacement suffers from two problems compared with your original:

1. The use of greedy repetition operators in place of non-greedy ones will result in incorrect results if there is more than one <a> element on a single line. The second greedy operator will capture everything between the first single or double quote up to the last one. And the third greedy operator will capture everything between the end of the first opening <a> tag on the line and the last closing </a> tag. If the <a> elements don't contain any child elements, you can improve the TextPad regex with
Find what: <a[^>]*href=['"]([^'"]*)['"][^>]*>([^<]*)</a>
Replace with: <a href="\1" target="_blank">\2</a>
However, it will still suffer from

2. Textpad's regexes are incapable of matching an arbitrary number of lines. One way to solve this is to replace newlines by something else, do your stuff, and then put the newlines back.

Furthermore, it is impossible, using TextPad regexes alone, to recognise text except when it contains some arbitrary text.

WildEdit (http://www.textpad.com/products/wildedit/) uses a far more powerful regex engine than TextPad, and has solutions for all of these problems.

Alternatively, you might use a script. TextPad doesn't support scripts, so you would have to use a suitable scripting language, such as Perl, Python, Ruby or Tcl.

There are many regular expression tutorials on the web, and you will find recommendations for some of them if you search this forum. A standard reference for regular expressions is

Friedl, Jeffrey E F
Mastering Regular Expressions, 3rd ed
O'Reilly, 2006
ISBN: 0-596-52812-4
http://regex.info/

But be aware of the remark above about the weakness of TextPad's recogniser.
whoisbambam
Posts: 3
Joined: Tue Oct 28, 2008 8:23 pm

Post by whoisbambam »

Wow. Ben, that was an extremely detailed reply. Thank you very much.

I imagine that you could make a video tutorial on teaching regex, you seem so knowledgeable.

If you do, let me know about it.

So probably it is not a good idea to use textpad in this particular case for this particular situaton.

thanks again,

steven
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

You're welcome. But I'm not sure that anyone would want to see any attempt of mine at video performance!

Whether you should use TextPad to solve this type of problem depends on how strongly you want to remain within the bounds of the editor, and how much you are prepared to intervene manually in the process of changing these files.

My own solution would be to write a script and run it from the command line.
whoisbambam
Posts: 3
Joined: Tue Oct 28, 2008 8:23 pm

Post by whoisbambam »

sure they would. just set your monitor to 800X600 at 16bit, use any decent noise-cancelling headset (make sure you setup thru the wizard and always do a test video before shooting), use this free software:

http://camstudio.org/

Convert to .swf for small file size, and you're done. I would use a timer and keep them under 10minutes each max, even if you have to do part1,2,3 etc just in case you decided to put them online.

save them as 5fps, 22khz mono, jpeg compression 80% (make sure you set that monitor to 16bit).

You may have to disable hardware acceleration on your video card, cause hardware accel. favors data movement from the video card to the monitor, not the monitor to the video card to the hard drive. So, to rebalance that, you disable hardware acceleration. and, if you are using a video card program (like ati catalyst control center), make sure you change that IN catalyst, and not by rt.clicking the desktop.

in any case, thanks for your explanation.

I lack the skills to write any script, so i am screwed. I have to proof read many html pages every month, and the writers never add that target blank, so i am going to be doing tons of copy/pasting.

:(

thanks again, Ben. You have been very gracious with your time.
Post Reply