Re: Getting unexpected results from sed inside AppleScript
Re: Getting unexpected results from sed inside AppleScript
- Subject: Re: Getting unexpected results from sed inside AppleScript
- From: "Mark J. Reed" <email@hidden>
- Date: Wed, 21 Nov 2007 12:52:06 -0500
The problems are these.
1. The regex is inappropriately greedy;
2. The AppleScript version sends the text as all one line.
So what happens is that the <a href="\(.*\)" is matching this whole string:
<a href=\"http://anothersite.com/woot\"><b>Restocked Traditional
Items.</b></a></p>
<p>and</p><p>a link <a href=\"http://anothersite.com/woot\"
and dutifully adding the style= attribute after it.
You could use
<a href="\([^"]\)*"
which will work as long as there are no quotation marks embedded in
the link (which should be the case, since quotation marks in a URL
should be represented as "), or you could use a tool that has
non-greedy quantifiers, like perl:
perl -pe 's/(<a href=".*?")/$1 style="color:black"/g'
If I may be forgiven a small sermon, however, I'd like to make two points:
1. No matter how convoluted you make your regex, it will be somewhat
fragile; HTML is not well suited to regex-based manipulation. You
might want to investigate some sort of tree-based XML transformation
solution.
2. Adding an explicit 'style="color:black"' is not doing anything to
keep you from having to do this sort of search and replace again in
the future. Much better to add a meaningful class name and then alter
the CSS stylesheet at whim, where you only have to change one place
instead of all of them. For instance, instead of 'style="color:
black"', you could do 'class="plain"', and then add ' a.plain { color:
black; }' to your stylesheet (external or otherwise). Of course, I'm
not sure what exactly you're doing, so that class name may not be
appropriate, and '{color: inherit;}' might be a better style, but I
think you get the idea.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden