Re: Satimage re_compile regex functions
Re: Satimage re_compile regex functions
- Subject: Re: Satimage re_compile regex functions
- From: has <email@hidden>
- Date: Sun, 28 Apr 2002 22:03:40 +0100
Luc Naets wrote:
>
It seems that the re-compile only works in smile and not for instance in
>
script debugger.
Don't know why that would be. (Though as I've said I've already found the
thing to be unreliable OMM.)
>
I have a lot of lines separated by a return from which I have to extract
>
only the lines that satisfy the regular expression
>
>
for instance
>
>
set mText to ("A22 24" & return & "A2 45" & return & "B34 44" & return &
>
"A34 67")
>
>
find text "(^A[[:digit:]]+ [[:digit:]]+)" in mText using "\\1" with regexp
>
>
>
it always returns only the first occurence of the xpression. When you did it
>
met Leonard's REMatch it returned all the matching lines.
>
>
Do you know how to do this.
Every implementation is different. With Satimage you have to use a repeat
loop. Slower, and less convenient, but it does the job:
======================================================================
set mText to ("A22 24" & return & "A2 45" & return & "B34 44" &
[NO-BREAK]return & "A34 67")
set resultList to {}
set stringOffset to 1
try
repeat
log {stringOffset, (mText's text stringOffset thru -1)}
set theMatch to (find text "(^A[[:digit:]]+ [[:digit:]]+)" in
[NO-BREAK](mText's text stringOffset thru -1) using "\\1" with regexp)
set stringOffset to stringOffset + (theMatch's matchPos) +
[NO-BREAK](theMatch's matchLen)
set resultList's end to theMatch's matchResult
end repeat
on error eMsg number eNum
if eNum is not in {1, -1728} then error eMsg number eNum
end try
resultList
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
>
I'm running on osX, that's why I have to find a solution for Leonard's
>
regex-osax.
You also have the option of using the terminal, though I remember Chris
Nebel saying this was s-l-o-w.
--
You may also find the info below useful (I don't know if it's been added to
the Smile manual or not as I'm still on my old copy of 1.8.4, so apologies
if this is now old news).
HTH
has
--------------------------------------------------------
***Regex Flags***
EXTENDED - Use the modern POSIX Extended Regular Expression syntax when
interpreting regex. If not set, use the obsolete POSIX Basic Regular
Expression syntax.
(Although I'm not sure why anyone would want to use obsolete syntax outside
of Backwards-Compatibility-With-Ancient-Unix-Scripts Land, which doesn't
really apply to any of us in Shiny-Smart-Applescripts Land.)
-------
NEWLINE - Use return-sensitive matching.
Without this flag:
- return is an ordinary character with no special meaning in either regular
expressions or strings
- a "^" anchor matches the beginning of the string and a "$" anchor matches
the end of the string.
With this flag:
- "[^
]" bracket expressions and "." never match return (returns can still
be deliberately matched using "\r")
- the "^" anchor additionally matches the beginning of a line in the string
and the "$" anchor additionally matches the end of a line in the string.
-------
ICASE - Ignore case in regex-based matching. (Use [case sensitive boolean]
for non-regex matching.)
Note: [case sensitive boolean] in "re_compile" doesn't do anything,
basically for this reason.
-------
NOSUB - quote: "Support for substring addressing of matches is not
required. The list resulted from successful match will be always empty. Use
it if you only want to know whether the match was found."
Personally, I'm not entirely sure what the value of this is: unless there's
some speed boost due to it not returning the additional info, there's no
advantage I can see in using it. i.e. Since you always have to wrap the
whole lot in a try statement anyway, you'd probably use something like:
try
match text.......
set foundMatch to true
on error
set foundMatch to false
end try
and ignore the returned result anyway. It'd be different if the thing
returned booleans when the nosub flag was set, but (for better or worse) it
doesn't.
I've also found some issues with this flag. Having tested it, I've found
that it's only useful/applicable with "find text", not "change" (which will
simply return the original string unchanged if flag is set). In addition,
there's a couple of glitches:
1. it won't work unless the user additionally specifies [using string]
(which I don't believe they should need to, given 2.)
2. when a match is found it returns {matchPos:-1.822131196E+9,
matchLen:1.921693996E+9, matchResult: [using string]}, rather than {} as
the above quote would indicate as the proper behaviour.
------
***Other stuff***
A cautionary note, really. In returning the matchPos property, the first
character of the string being matched is counted as 0. I guess this is the
nature of the beast, so changing this behaviour may not be a good idea, but
it may catch out unwary Applescripters who are used to the first character
of a string being counted as 1.
--------------------------------------------------------
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
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.