Re: Regex in OS X
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.
References: | |
| >Regex in OS X (From: Hanaan Rosenthal <email@hidden>) |