Re: Help with grep
Re: Help with grep
- Subject: Re: Help with grep
- From: Axel Luttgens <email@hidden>
- Date: Sat, 26 Dec 2015 10:13:55 +0100
> Le 25 déc. 2015 à 22:18, Bert Groeneveld a écrit :
>
> Hi, is there a way to simplify this statement using grep? Can anyone post example code? (I have satimage.osax installed).
>
> I want to check if a string starts wit one or two digits followed by a plus sign followed by one digit followed by a space followed by the word “gratis”.
>
> if (_tempData as text) starts with "1+1 gratis" or (_tempData as text) starts with "2+1 gratis" or (_tempData as text) starts with "3+1 gratis" or (_tempData as text) starts with "2+2 gratis" or (_tempData as text) starts with "5+5 gratis" or (_tempData as text) starts with "1+2 gratis" or (_tempData as text) starts with “10+2 gratis” then
>
> Thanks in advance for any help, Bert.
Hello Bert,
Unless I’m wrong, Smile [1] may make use of extended REs (see the man page for re_format(7)), in which case following pattern should be fine for both grep and Smile:
^[0-9][0-9]?\+[0-9] gratis
A brief explanation:
^: the beginning of the text
[0-9]: one digit
[0-9]?: zero or one digit
\+: a plus sign (must be escaped, since + has a special meaning in REs)
gratis: some literal text
Since that pattern is going to be used from AppleScript, it should be properly escaped wrt AppleScript’s rules, as in:
set reg_exp to "^[0-9][0-9]?\\+[0-9] gratis"
Assuming you are only interested in a match/doesn’t match result, here follows a grep-based sample code:
on check_string(reg_exp, test_string)
try
do shell script "/usr/bin/grep -E " & quoted form of reg_exp & "<<<" & quoted form of test_string
return true
on error
return false
end try
end check_string
check_string("^[0-9][0-9]?\\+[0-9] gratis", "12+5 gratis deel")
HTH,
Axel
[1] Details for Smile’s regular expressions are provided at http://www.satimage.fr/software/en/smile/text/reg_exp_syntax.html
_______________________________________________
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