In my new situation, I want to replace spaces with %20 but only in PART of the selected text. I have text like this:
[Joe J. Smith]<j...@joesmith.com><subject=Sign me up!><body=I want to subscribe to the newsletter.>
(There would be hundreds of lines of text selected, with various email addresses and “subject=“ text. Maybe 1 percent of the selected text contains text that I want to change)
Here, I want to change “subject=Sign me up!” to “subject=Sign%20me%20up!”
I can’t do this:
replace " " using "%20" searching in selection of text window 1 of myDocument
… because that would change “Joe J. Smith” to “Joe%20J%20Smith” (among other things. There is a lot of text in the selection that should retain its spaces rather than convert to %20.
What I want to do is (somehow) do a Find (searching in the original selection), looking for “subject=“ and then some GREP to get everything up to the closing “>”. I was hoping that by adding “with selecting match” I could select EVERY matching string (a discontiguous selection) and then search in this new selection for spaces and replace them all in one move with %20. Basically I’m trying to change the selection to be only the parts I want to make changes in.
My problem is, when I do a Find and “with selecting match” it only finds and selects the FIRST instance. I can then do my replacing within that text, but that only changes the first instance. What I’m doing now is looping through the document, changing the first instance that the Find finds, and then coming around on the loop to pick up the next one, and the next one, etc. It’s a little on the slow side and I have to set the number of loops to something pretty high to be sure I cover the most extreme situation (which is hard to predict so I am doing 1000 loops). A modification would be to figure out how many loops I have to make, which I think I could do, but I don't want to loop at all.
I know I can do a Find manually in BBEdit and choose the Find All option. Can I make use of the results of a Find All in a script? Maybe do a Find… with a Find All option, and then do my replace on the results of that Find?
Maybe my approach is all wrong here. Even if it is, it would be useful to know whether I can do a Find… to create a discontiguous selection. If I can, my problem goes away— I do a Find... within the original selection, I select the results box that Find... (producing a new, smaller selection), I run a single “replace” on that new selection. If I can’t do that, I wonder what is the right way to go.
Thanks for any assistance you can give. Basically I'm trying to get out of doing a loop.
In my new situation, I want to replace spaces with %20 but only in PART of the selected text. I have text like this:
[Joe J. Smith]<j...@joesmith.com><subject=Sign me up!><body=I want to subscribe to the newsletter.>
...
Chris, did you mean:$newSubject =~ s!\h!\%20!g;
rather than
$newSubject =~ s!\h!\$20!g;
On Feb 17, 2023, at 12:38, Bruce Van Allen <b...@cruzio.com> wrote:
Only happened because I always study your great scripts to learn from them!