### Question 13.7

I need some code to do regular expression and wildcard matching.

Make sure you recognize the difference between classic regular expressions (variants of which are used in such Unix utilities as `ed` and `grep`), and filename wildcards (variants of which are used by most operating systems).

There are a number of packages available for matching regular expressions. Most packages use a pair of functions, one for ``compiling'' the regular expression, and one for ``executing'' it (i.e. matching strings against it). Look for header files named `<regex.h>` or `<regexp.h>`, and functions called `regcmp`/`regex`, `regcomp`/`regexec`, or `re_comp`/`re_exec`. (These functions may exist in a separate regexp library.) A popular, freely-redistributable regexp package by Henry Spencer is available from ftp.cs.toronto.edu in pub/regexp.shar.Z or in several other archives. The GNU project has a package called rx. See also question 18.16.

Filename wildcard matching (sometimes called ``globbing'') is done in a variety of ways on different systems. On Unix, wildcards are automatically expanded by the shell before a process is invoked, so programs rarely have to worry about them explicitly. Under MS-DOS compilers, there is often a special object file which can be linked in to a program to expand wildcards while `argv` is being built. Several systems (including MS-DOS and VMS) provide system services for listing or opening files specified by wildcards. Check your compiler/library documentation.

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback