• 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 OS X
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Regex in OS X


  • Subject: Re: Regex in OS X
  • From: Christopher Stone <email@hidden>
  • Date: Fri, 6 Feb 2004 15:46:31 -0600

At 15:59 -0500 02/06/2004, Hanaan Rosenthal wrought:

I know Satimage has some regular expressions to handle such a thing. Would anyone (Emmanuel) have some code that will return the line that contains a match at the start of the line. Example:
Search string: "Macdonalds"
Result is the line:
"Macdonalds McDonalds"
Since it starts with the search string.
______________________________________________________________________

Hey Hanaan,

This is one is particularly easy:

----------------------------------------------------------------------
set theText to "
GM General Motors
Macdonalds McDonalds
MicroSoft Microsoft
"

try
set findIt to find text "^Macdonalds.+" in theText with regexp and string result without case sensitive
on error
set findIt to false
end try
----------------------------------------------------------------------

The pattern is: "^Macdonalds.+"

^ == beginning of line
. == any character
+ == one or more

----------------------------------------------------------------------

If you're operating directly on a file you can use that in the reference:

set theFile to alias "some:file:path"

try
set findIt to find text "^Macdonalds.+" in theFile with regexp and string result without case sensitive
on error
set findIt to false
end try

----------------------------------------------------------------------

The Satimage osax does line-based regex by default; however, you can make it greedy by using flags:

Line regex:

try
set findIt to find text "" in theText regexpflag {"EXTENDED", "NEWLINE", "ICASE"} with regexp and string result
on error
set findIt to false
end try


Greedy regex:

try
set findIt to find text "" in theText regexpflag {"EXTENDED", "ICASE"} with regexp and string result
on error
set findIt to false
end try

Using a try block is necessary, because "find text" will throw an error if it doesn't find anything.

[CC'd due to the slowness of the list.]


Chris
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Regex in OS X
      • From: Hanaan Rosenthal <email@hidden>
References: 
 >Regex in OS X (From: Hanaan Rosenthal <email@hidden>)

  • Prev by Date: Regex in OS X
  • Next by Date: Re: Regex in OS X
  • Previous by thread: Regex in OS X
  • Next by thread: Re: Regex in OS X
  • Index(es):
    • Date
    • Thread