• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: regex in Satimage OSAX doesn't work the way I expected
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regex in Satimage OSAX doesn't work the way I expected


  • Subject: Re: regex in Satimage OSAX doesn't work the way I expected
  • From: Christopher Stone <email@hidden>
  • Date: Tue, 02 Oct 2012 22:42:01 -0500

On Oct 02, 2012, at 17:21, Jim Brandt <email@hidden> wrote:
Yea, I discovered that while changing other script code. Thanks for pointing it out. I'm suppressed it didn't fail with an error since "regex" isn't a defined variable at that point.
______________________________________________________________________

Hey Jim,

That problem with writing regex instead of with regexp was a big issue for me when I first started using the Satimage.osax.

I ended up just creating several handlers in find.lib.scpt and embedding them in my Script Debugger templates.  I then use Typinator or SD's own text-expansions to emplace the handler calls, so I never have to type them.

My basic change handler:

----------------------------------------------------------------------
on cng(findText, changeText, srcData)
change findText into changeText in srcData with regexp without case sensitive
end cng
----------------------------------------------------------------------

I rely on regex switches instead of a list of properties to change case sensitivity, etc (although as you can see the default for the handler is case-insensitive).

set n to "Now is the time for all good men to come to the aid of their country."

set nRslt to cng("(?-i)N", "•", n) of me

set o to "Now is the time for all good men to come to the aid of their country.
Four score and seven years ago...
"

set oRslt to cng("(?m-i)is.+", "•", o) of me

set x to "Macintosh HD:HD1:Misc: New: Fix:MyFolder: HTML"

cng("^(?:[^:]+:){5}([^:]+).+", "\\1", x) of me

# splittext is another Satimage.osax command.
set _dir to splittext x using ":"
if length of _dir ≥ 2 then
set _dir to item -2 of _dir
end if

Note that find text and change will operate on a list of strings or a single string.

Note also that find text throws an error if something is not found which is why the handlers return false on error.

I can actually do both a test for a found string and get the string in one go:

set n to "Now is the time for all good men to come to the aid of their country."

if fnd("Now", n, true, false, true) of me ≠ false then
  set foundString to result
end if

Although one of the handlers returns a straight boolean.

Appended is the whole library minus the awk, sed, and perl stuff.

--
Best Regards,
Chris

______________________________________________________________________

Default handler call strings:

cng("", "", srcData) of me
fnd("", srcData, false, false, true) of me
fndUsing("()", "\\1", srcData, false, true, false, false, true) of me

Sometimes I forget the order of the longer handler's parameters, so I also have Typinator abbreviation to show the syntax.

------------------------------------------------------------------------------------------------
--» cng()
------------------------------------------------------------------------------------------------
# Modified: 2011-08-31 : 20:16
# Satimage.osax --> 'change'
------------------------------------------------------------------------------------------------
on cng(findText, changeText, srcData)
  change findText into changeText in srcData with regexp without case sensitive
end cng

------------------------------------------------------------------------------------------------
--» fnd()
------------------------------------------------------------------------------------------------
# Modified: 2011-08-31 : 20:16
# Satimage.osax --> 'Find Text'
------------------------------------------------------------------------------------------------
on fnd(findStr, srcData, caseSensitive, allOccurrences, stringResult)
  try
    set findResult to find text findStr in srcData ¬
      case sensitive caseSensitive ¬
      all occurrences allOccurrences ¬
      string result stringResult ¬
      with regexp
    return findResult
  on error
    return false
  end try
end fnd

------------------------------------------------------------------------------------------------
--» fndBool()
------------------------------------------------------------------------------------------------
# Modified: 2011-09-12 : 16:16
# Satimage.osax 'Find Text' with a boolean result
------------------------------------------------------------------------------------------------
on fndBool(findStr, srcData, caseSensitive, allOccurrences, stringResult)
  try
    set findResult to find text findStr in srcData ¬
      case sensitive caseSensitive ¬
      all occurrences allOccurrences ¬
      string result stringResult ¬
      with regexp
    return true
  on error
    return false
  end try
end fndBool

------------------------------------------------------------------------------------------------
--» fndUsing()
------------------------------------------------------------------------------------------------
# Modified: 2011-08-31 : 20:16
# Satimage.osax --> 'Find Text' with Capture
------------------------------------------------------------------------------------------------
on fndUsing(fndStr, returnStr, srcData, caseSensitive, regExFlag, wholeWord, allOccurrences, stringResult)
  try
    set findResult to find text fndStr ¬
      in srcData ¬
      case sensitive caseSensitive ¬
      regexp regExFlag ¬
      whole word wholeWord ¬
      using returnStr ¬
      all occurrences allOccurrences ¬
      string result stringResult
    return findResult
  on error # errMsg number errNum
    return false
  end try
end fndUsing

------------------------------------------------------------------------------------------------
--» fndSyn()
------------------------------------------------------------------------------------------------
# Modified: 2011-08-31 : 20:16
# Satimage.osax --> 'Find Text' with Capture and Syntax
------------------------------------------------------------------------------------------------
on fndSyn(fndStr, returnStr, srcData, caseSensitive, regExFlag, wholeWord, allOccurrences, stringResult, _syntax)
  try
    set findResult to find text fndStr ¬
      in srcData ¬
      case sensitive caseSensitive ¬
      regexp regExFlag ¬
      whole word wholeWord ¬
      using returnStr ¬
      all occurrences allOccurrences ¬
      string result stringResult ¬
      syntax _syntax
    return findResult
  on error # errMsg number errNum
    return false
  end try
end fndSyn
------------------------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >regex in Satimage OSAX doesn't work the way I expected (From: Jim Brandt <email@hidden>)
 >Re: regex in Satimage OSAX doesn't work the way I expected (From: "email@hidden" <email@hidden>)
 >Re: regex in Satimage OSAX doesn't work the way I expected (From: Jim Brandt <email@hidden>)

  • Prev by Date: Re: Referencing a Message in Mail 6.1 - Huh?
  • Next by Date: Re: Referencing a Message in Mail 6.1 - Huh?
  • Previous by thread: Re: regex in Satimage OSAX doesn't work the way I expected
  • Next by thread: Re: regex in Satimage OSAX doesn't work the way I expected
  • Index(es):
    • Date
    • Thread