[X-Unix] Grep and backreferences with BBEdit

James Avgeris jima at itempo.com
Tue Oct 12 11:48:26 PDT 2004


On Oct 12, 2004, at 11:12 AM, Nick Scalise wrote:

> Hello,
>
> I do not have a strictly unix question. I am very weak at grep and 
> could use some rudimentary help.
>
> In BBEdit 6.5, I am wanting to do a search and replace for all 
> occurances of <a href="name.jpg">, where 'name' is the variable. I 
> want to add a images/ to the beginning of the 'name' variable.
>
> So, I think I have the first part figured out, I find <a 
> href="[a-zA-Z0-9+]+.jpg"> using grep in BBEdit's find dialog, this 
> seems to produce the desired result. Please let me know if there is a 
> better way.
>
> Where I am lost is the backreference part. What is the pattern I would 
> add in the replace dialog that would add 'images/'  so that the 
> original <a href='name.jpg'> would end up <a href="images/name.jpg">
>
> TIA
> --
> Nick Scalise
> nickscalise at mac.com

Nick, here's a GREP pattern you could use in BBEdit:

(<a href=")([^"]+">)

Then in the replace dialog it would be:

\1images/\2

the \1 represents the match between the first parentheses, and the \2 
is the second. The [^"]+ matches anything that is not a double quote, 
which MAY not have made a difference in this case but is better 
(assuming you can rely on the quotes being there) because GREP is 
greedy and will match as it can so you need to be restrictive.



More information about the X-Unix mailing list