BBEdit’s grep replacement feature is not as robust as most; most people get smart and learn perl or another language with a deeper definition of features, but I don’t have that time today. What I need is to 1) replace a series of text that is randomly identified and 2) number it sequentially.
Now in normal grep, this would be a bit easier than expected by applying a Regular Expression Counter to your replacement:
e%0>
Would sequentially add a number greater to zero. Brilliant! Unsupported in BBEdit!
Now, again, you could code in a different language and accomplish this much easier if you have that experience, but for me, the trick was to step around BBEdit’s limitation with line number.
0 <li>Brady</li>
1 <li>Brady</li>
2 <li>Brady</li>
3 <li>Brady</li>
So that the line numbers are remove from the left, and the text ’60′ is replaced by them using a specific grep pattern:
<li>Brady 0</li>
<li>Brady 1</li>
<li>Brady 2</li>
<li>Brady 3</li>
As I noted before, Grep in BBEdit is limited, although there’s power to be had (and I recommend Cari D. Burstein’s overview). While I can’t call much in the replacement command, I can call data from the find portion and pass it along to the replacement area.
In the case of the find:
(\d*) <li>Brady</li>
I’ve used parenthesis to tell BBEdit these are ‘groups’ to be recognized later on; these groups are counted as group 1, then group 2, then group 3, and so on. Inside these parenthesis I use the \d command to mean ‘a numerical digit’ and the asterisk to mean ‘as many digits as I’d like’. I’ve now set myself up to search for the numerical line number as a group, and reuse it in my replace.
Now I’m ready to replace my content:
<li>Brady \1</li>
The \1 means ‘replace this area with content from the first group you called in the search’. The end result is that the search number is moved here, and since I didn’t add any command for the group to be repeated at the beginning of the line, the first line numbering is removed.
One of our entry, and rushed, business ventures under my scope is for my Boss’ beautiful new line of Jewelry, Amber Marie Bently Jewelry. She’s recently designed a new series of pieces, and I’d like those designs to be seated at the front of the web list. Since I’ve used a Javascript slider, I’ll have to put all the list content at the top, and then dynamically change the sequential order of each full size image and each Thumbnail… annoying by hand, less than a second with Grep replacement.
0 <li><a href="#"><img id="thumb57" src="thumbnails/test1.jpg" width="60" height="42" alt="thumbnail 60"></a></li>
(\d*) <li><a href="#"><img id="thumb(\d*)" src="(.*)" width="60" height="42" alt="thumbnail (\d*)"></a></li><li><a href="#"><img id="thumb\1" src="\3" width="60" height="42" alt="thumbnail \1"></a></li>…and that’s it. 98 thumbnails and 98 full size images magically ordered on the fly in real time.