[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.1 How do I compare strings using wildcards?

The answer to that depends on what exactly you mean by `wildcards'.

There are two quite different concepts that qualify as `wildcards'. They are:

Filename patterns

These are what the shell uses for filename expansion (`globbing').

Regular Expressions

These are used by editors, grep, etc. for matching text, but they normally aren't applied to filenames.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.1.1 How do I compare strings using filename patterns?

Unless you are unlucky, your system should have a function fnmatch() to do filename matching. This generally allows only the Bourne shell style of pattern; i.e. it recognises ‘*’, ‘[...]’ and ‘?’, but probably won't support the more arcane patterns available in the Korn and Bourne-Again shells.

If you don't have this function, then rather than reinvent the wheel, you are probably better off snarfing a copy from the BSD or GNU sources.

Also, for the common cases of matching actual filenames, look for glob(), which will find all existing files matching a pattern.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.1.2 How do I compare strings using regular expressions?

There are a number of slightly different syntaxes for regular expressions; most systems use at least two: the one recognised by ed, sometimes known as `Basic Regular Expressions', and the one recognised by egrep, `Extended Regular Expressions'. Perl has it's own slightly different flavour, as does Emacs.

To support this multitude of formats, there is a corresponding multitude of implementations. Systems will generally have regexp-matching functions (usually regcomp() and regexec()) supplied, but be wary; some systems have more than one implementation of these functions available, with different interfaces. In addition, there are many library implementations available. (It's common, BTW, for regexps to be compiled to an internal form before use, on the assumption that you may compare several separate strings against the same regexp.)

One library available for this is the ‘rx’ library, available from the GNU mirrors. This seems to be under active development, which may be a good or a bad thing depending on your point of view :-)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated on September, 10 2007 using texi2html 1.77.