There are two basic sets of pattern matching: files and strings
fnmatch() is used to match wildcards like ? and * in file name patterns.
regcomp(), regexec(), regfree() are called in that order to build, then execute, then release resources for
grep and
egrep like pattern matching.
Generally you are better off to use these library calls than to roll your own. If you already can use
ls pattern matching it is easy to use the
fnmatch call.
The code structure for emulating what the grep command does is a little more complex.
If you remember, grep and egrep have a lot of options. Since they are implemented by the regex family of calls, the calls are more complex. Options for constructing the resources regcomp (regular expression compile) supports several. The regex command supports the others.
There is also the PCRE library that perl regex uses. If you are a perl user, consider that library.
Don't try to roll your own if you've never gotten fully acquainted with a regex library. If you must, read Russ Cox to get an idea how to proceed.
Implementing Regular Expressions
Site has howtos