Chapter 23. String Matching

There is more to comparing strings than strcmp() or even strncmp(). Linux provides several general string-matching functions that make your programming tasks simpler. We start with the simple tasks and then cover the more complex ones.

Globbing Arbitrary Strings

Chapter 14 explains how to glob file names using the glob() function, but people used to globbing capabilities sometimes wish to apply them to other sorts of strings. The fnmatch() function allows you to apply globbing rules to arbitrary strings:

#include <fnmatch.h>

int fnmatch(const char *pattern, const char *string, int flags);

The pattern is a standard glob expression with four special characters, modified by the flags argument:

*

Matches any string, including ...

Get Linux Application Development, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.