On Aug 23, 2015, at 22:00, 'Holger B' via BBEdit Talk <bbedit@googlegroups.com> 
wrote:
> See a portion of the file below. The amount of categories as well as tags are 
> the ones that can vary and getting it in one pass could replace both of them. 
> I suppose it's not possible in one pass though, but am curious if I'm missing 
> something.
______________________________________________________________________

Hey Holger,

Essentially you're wanting to find a variable string, massage that string a 
fair amount into its final form, and replace it.

The less regular the string is the more difficult it is to manage with a 
single-pass regex find/replace.

It becomes much easier to find the string, do all necessary adjustments, 
replace it, and repeat.

Try this AppleScript and see if it does what you're wanting to accomplish.

-------------------------------------------------------------------------------------------
tell application "BBEdit"
  set bbeditApp to a reference to it
  set foundRec to {found:true}
  
  tell front text window's text
    select insertion point before it
    
    repeat while found of foundRec is true
      
      set foundRec to find "^categories:(\\s+^[[:blank:]]+- \\b[\\w ]+\\b)+" 
options ¬
        {search mode:grep, case sensitive:false} with selecting match
      
      if found of foundRec is true then
        tell bbeditApp
          set newText to replace "\\s+-[[:blank:]]+(\\b[\\w ]+\\b)" using "\\1, 
" searchingString (get contents of text of found object of foundRec) options 
{search mode:grep, case sensitive:false}
          set newText to replace "(categories:)(.+?), $" using "\\1 \\2" 
searchingString newText options {search mode:grep, case sensitive:false}
        end tell
        set (contents of found object of foundRec) to newText
      end if
      
    end repeat
    
  end tell
end tell
-------------------------------------------------------------------------------------------

If I was doing this job frequently on fairly hefty files I'd probably write a 
Perl text filter to speed things up.

--
Best Regards,
Chris

-- 
This is the BBEdit Talk public discussion group. If you have a 
feature request or would like to report a problem, please email
"supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bbedit+unsubscr...@googlegroups.com.
To post to this group, send email to bbedit@googlegroups.com.

Reply via email to