Date: Sun, 9 Nov 1997 00:56:01 +0100
Reply-To: REXX Programming discussion list <REXXLIST@UGA.CC.UGA.EDU>
Sender: REXX Programming discussion list <REXXLIST@UGA.CC.UGA.EDU>
From: F-J Wirtz <fjw@ECOTOPIA.OCHE.DE>
Organization: roaring silence unlimited
Subject: Re: regular expression matching
>> Is there anything in O/S2 REXX that can do regular expression pattern
>> matching
>Perhaps a more interesting question is: does anyone have a standard REXX
>implementation of a function that implements a regular expression
>comparison of a string -- e.g. a function RegExp(Needle, Haystack [,
>Start]) that works like POS(Needle, Haystack [, Start])?
As REXX as a scripting language is intented to be the *glue* between the
operating system commands there maybe a way around the lack by using
programs which do the job. At least it is possible with the general
available utility 'grep' to find out, wether a regexp matches another
string or not (the script eventually may be improved by replacing the
explicit file with pipes):
/* regexpmatch -- by fjw@ecotopia.oche.de
PURPOSE:
check, wether a needle is found in the haystack by
using the standard gnu utility grep for regular expression match
RETURNS:
true (1) if found or false (0) if not
INPUT:
needle must be specified in ""
EXAMPLE:
s) called from commandline
regexpmatch "[fg]o[oa]ls a" no fools around
returns 1
regexpmatch "[fg]o[oa]ls f" no goals found
returns 1
regexpmatch "[fg]o[oa]ls a" no foams about
returns 0
b) called as a function in another script
needle="[fg]o[oa]ls a"
haystack=" no fools around"
arglist=""""needle"""" haystack
found=regexpmatch( arglist)
REQUIRES:
grep (found as GNU utility)
*/
signal on novalue
parse source myos mytype myfullname
parse value filespec( "name", myfullname) with myname ".cmd"
haystackfile=myname".tmp"
parse arg """"needle"""" haystack
select
when (mytype == "COMMAND") then
do
called_as_function = 0
end
when (mytype == "FUNCTION") then
do
called_as_function = 1
end
otherwise /* SUBROUTINE */
called_as_function = 0
end
if \called_as_function then
do
say "needle="needle
say "haystack="haystack
end
call stream haystackfile, "command", "open write"
call lineout haystackfile, haystack, 1
call stream haystackfile, "command", "close"
address cmd '@grep -b -e "'needle'" 'haystackfile'>NUL'
/* grep returns with exit code 0 if found and 1 if not found */
found = (rc == 0)
if \called_as_function then
do
if (found) then
answer = "is"
else
answer = "is not"
say "needle "answer" found in haystack"
end
exit found
The following script shows the usage from another script
/* testregexpmatch -- */
say "input="arg(1)
parse arg """"needle"""" haystack
say "needle="needle
say "haystack="haystack
arglist=""""needle""""haystack
say "new arglist="arglist
found=regexpmatch( arglist)
say "found="found
--
Franz-Josef Wirtz, Physical Chemist, PhD, e-mail: fjw@ecotopia.oche.de
Juelicher Str. 299, D-52070 Aachen, Germany, Phone: 49-241-962005