On 04/15/2017, at 07:44, Jean-Christophe Helary
<[email protected] <mailto:[email protected]>>
wrote:
> It looks like I'm trying to reinvent the wheel, but I can find a way to do
> this:
>
> 1) replace ^\s with nothing
> 2) replace \s$ with nothing
>
> in a single string
Hey Jean-Christophe,
There are two keys to doing this.
A) Correct regex syntax (which Bruce kindly provided).
B) The searchingString parameter of the replace command.
------------------------------------------------------------------------------
set pathString to " /path/to/my file.stuff "
tell application "BBEdit"
set newString to replace "^\\s*|\\s*$" using "" searchingString pathString
options {search mode:grep}
end tell
------------------------------------------------------------------------------
For my own use I always employ the Satimage.osax for this sort of thing.
------------------------------------------------------------------------------
set pathString to " /path/to/my file.stuff "
set newString to cng("^\\s*|\\s*$", "", pathString) of me
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on cng(_find, _replace, _data)
change _find into _replace in _data with regexp without case sensitive
end cng
------------------------------------------------------------------------------
The Satimage.osax is simply faster and more versatile for this sort of thing.
(Handlers hidden in a library on my system.)
But BBEdit is no slouch either and has long had the ability to replace in a
string (in addition to the text of a document). I can't remember now in what
version that gem was introduced.
This job can also be handled nicely by AppleScriptObjC these days:
------------------------------------------------------------------------------
# Tags: @Applescript, @Script, @ASObjC, @BBEdit, @RegEx, @ccstone
------------------------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
------------------------------------------------------------------------------
set pathString to " /path/to/my file.stuff "
set newStr to its cngStr:"^\\s*|\\s*$" intoString:"" inString:pathString
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on cngStr:findString intoString:replaceString inString:dataString
set anNSString to current application's NSString's
stringWithString:dataString
set dataString to (anNSString's ¬
stringByReplacingOccurrencesOfString:findString
withString:replaceString ¬
options:(current application's NSRegularExpressionSearch) range:{0,
length of dataString}) as text
end cngStr:intoString:inString:
------------------------------------------------------------------------------
--
Take Care,
Chris
--
This is the BBEdit Talk public discussion group. If you have a
feature request or would like to report a problem, please email
"[email protected]" 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/bbedit.