Regular expression in BBEdit: find and replace within a context
I came across a generated HTML file with reference to thousands of .aif audio files that had spaces in their names. I had no problem correcting the file names themselves, using a perl script: replacing " " to "_". But updating the links within the html file was a problem, I have very limited knowledge of regular expression, and the little that I know, I have found it very useful. My problem in this case is I need to replace " " to "_" with a link, not globally:I first tried this:
Find this:
(a href="[^ ]+) <--note a space after the ending)Replace with:
\1_
Very crude...it would work to a point but then it started replacing spaces that are in non link text for example, "<a href="abc.aif">the_link..."which I didn't want to happen.
It took a while before I managed to come up with something like this:
Find this:
(^<a href="[^ ]+) ([^ ]+\.aif">$)Replace with:
\1_\2
This would of course only look for the first "_" within the link. I had to run it multiple times in order to change names like "abc def.aif" to "abc___def.aif". I also came across some names that are like "ab cd ef.aif". I changed my grep patten in order to accommodate this.
Find this:
(<a href="[^ ]+) ([^ ]*) ([^ ]+\.aif">) <--note the 2nd matchReplace with:
\1_\2_\3
I will probably come back here to check how i did it. If you have a better solution, please let me know.
Comments?
Leave a comment regarding “Regular expression in BBEdit: find and replace within a context”
Recent articles in osXosX: ASR - Apple Software RestoreosX: Bootable OSX cd osX: File locations osX: Hdiutil osX: OS 9 partition on new 2003 hardware |
bio-degradable