pattern matching
Jef Poskanzer
pokey at well.UUCP
Fri May 19 04:55:25 AEST 1989
In the referenced message, mhb at hcx.uucp (MARK H BOTNER) wrote:
}Can anybody help me with pattern matching?
}P.S. if you have any answer or a subroutine for me, please mail it to me.
}My address is:
} mhb at cseg.uucp
That address is useless, so I'm posting this. This is free software, no
copyright or trade secret restrictions whatsoever.
---
Jef
Jef Poskanzer jef at helios.ee.lbl.gov ...well!pokey
/*
** Simple pattern matcher. Does ?, *, and [], much like various shells.
**
** by Jef Poskanzer
*/
amatch( s, p )
register char *s, *p;
{
for ( ; *p != '\0'; p++, s++ )
{
if ( *p == '*' )
{
if ( *++p == '\0' )
return 1;
for ( ; *s != '\0'; s++ )
if ( amatch( s, p ) )
return 1;
return 0;
}
if ( *s == '\0' )
return 0;
if ( *p == '?' )
continue;
if ( *p == '[' )
{
int negcc = 0, ccmatch = 0;
char prevc;
if ( *++p == '!' )
{
negcc = 1;
p++;
}
for ( ; *p != ']'; p++ )
{
if ( *p == '\0' )
{
fprintf( stderr, "amatch: missing ']'\n" );
return 0;
}
if ( *p == '-' )
{
if ( prevc <= *s && *++p >= *s )
ccmatch = 1;
}
else if ( *p == *s )
ccmatch = 1;
prevc = *p;
}
if ( ( ccmatch && ! negcc ) || ( negcc && ! ccmatch ) )
continue;
return 0;
}
if ( *p != *s )
return 0;
}
return *s == '\0';
}
More information about the Comp.unix.questions
mailing list