glob pattern matching algorithm wanted
Author Message
glob pattern matching algorithm wanted

Can anyone tell me where I may find a function that will match a string
with a glob pattern (like the one used in sh/ksh)?

Ik Su Yoo
RRC-4F728, x5764

--

Ik Su Yoo
RRC-4F728, x5764

Mon, 29 Jul 1996 20:20:06 GMT
glob pattern matching algorithm wanted
Earlier today I posted a recursive function to match glob patterns,
but it turned out to have a bug in it, and the companion inrange()
function too. So I hope the code below *is* correct... At least I
tried it out for a number of tricky cases (eg. "a*[x-z]*" for pat).

int inrange (char **pat, char ch)
{
int in = 0;
char l, r;
do
{
if ((r = *(*pat)++) == '-')
{
r = *(*pat)++;
if (l <= ch && ch <= r) in = 1;
}
else
{
if (ch == (l = r)) in = 1;
}
}
while (**pat != ']');
return in;
}

int match (char *pat, char *str)
{
if (!*str)
{
if (*pat == '*') return match (pat+1, str); else return !*pat;
}
switch (*pat)
{
case '\0': return !*str;
case '*' : if (match (pat+1, str)) return 1;
return match (pat, str+1);
case '?' : return match (pat+1, str+1);
case '[' : return inrange (&pat, *str) && match (pat+1, str+1);
default  : return *pat == *str && match (pat+1, str+1);
}
}

---
^^

University of Twente                 =x=  \        tel. +31 53 893747
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/

Tue, 30 Jul 1996 22:41:22 GMT
glob pattern matching algorithm wanted
If you are working in a unix environment you might try:

/* -- Cut Here -- */

#include <stdio.h>

#ifndef TRUE
#   define TRUE 1
#endif

#ifndef FALSE
#   define FALSE 0
#endif

int main(argc, argv)
int argc;
char **argv;
{
re_comp("^[a-z][^a-z]");

if(re_exec("a2"))
printf("String matches regular expression\n");
else
printf("String does not match regular expression\n");

if(re_exec("aa"))
printf("String matches regular expression\n");
else
printf("String does not match regular expression\n");

return(0);

Quote:
} /* main */

/* -- Cut Here -- */

Here is the relevant man page:

REGEX(3)               C LIBRARY FUNCTIONS               REGEX(3)

NAME
regex, re_comp, re_exec - regular expression handler

SYNOPSIS
char *re_comp(s)
char *s;

re_exec(s)
char *s;

DESCRIPTION
re_comp() compiles a string into an internal  form  suitable
for  pattern matching.  re_exec() checks the argument string
against the last string passed to re_comp().

re_comp() returns a NULL pointer if the string  s  was  com-
piled  successfully;  otherwise a string containing an error
message is returned.  If re_comp() is passed  0  or  a  null
string,  it  returns without changing the currently compiled
regular expression.

re_exec() returns 1 if the string s matches  the  last  com-
piled  regular expression, 0 if the string s failed to match
the last compiled regular expression, and -1 if the compiled
regular  expression  was  invalid  (indicating  an  internal
error).

The strings passed to both re_comp() and re_exec() may  have
trailing or embedded NEWLINE characters; they are terminated
by null characters.  The regular expressions recognized  are
described  in  the  manual  entry for ed(1), given the above
difference.

ed(1), ex(1), grep(1V)

DIAGNOSTICS
re_exec() returns -1 for an internal error.

re_comp() returns one of the following strings if  an  error
occurs:

No previous regular expression

Regular expression too long

unmatched \(

missing ]

too many \(\) pairs

Sun Release 4.1    Last change: 6 October 1987                  1

REGEX(3)               C LIBRARY FUNCTIONS               REGEX(3)

unmatched \)

Sun Release 4.1    Last change: 6 October 1987                  2

Hope this helps!

/*****************************************************************************
* John F. Dumas         |  finger \$USER | grep `echo putz | tr utpz earl` \

* Programmer / Analyst  |  echo '0H &#* #(!725 4^ 48*4!' | tr \

* Houston, Tx           |  2> /dev/null
*****************************************************************************/

Sat, 03 Aug 1996 22:10:57 GMT
glob pattern matching algorithm wanted

Quote:
>If you are working in a unix environment you might try:

>#include <stdio.h>

[unused boolean defs snipped]
Quote:
>int main(argc, argv)
>   int argc;
>   char **argv;
>{
>   re_comp("^[a-z][^a-z]");

>   if(re_exec("a2"))
>      printf("String matches regular expression\n");

[snip]

These functions are for matching *regular expressions* not *glob patterns*.
For a glob pattern matcher, dig a bit in comp.lang.c's recent history; I
posted one a few days ago.

---
^^

University of Twente                 =x=  \        tel. +31 53 893747
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/

Mon, 05 Aug 1996 21:28:18 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages