Thursday 26 February 2015

Wildcard pattern matching in C

Welcome to Logically Proven blog.

This post demonstrates how to match a wildcard pattern against a particular string in C programming.

To achieve this we are using the function fnmatch which is in-built declared in the header fnmatch.h

Syntax:

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

This function tests whether the string is matching against the given pattern. This function returns zero if the string match with the given pattern else the function returns non-zero value FNM_NOMATCH.

The both  string and pattern are strings.

The third argument flags is a combination of flag bits that alter the details of matching which means you can change the way of matching the string by passing the flag bits.

The following are the available flags -

FNM_FILE_NAME

This flag treats the '/' character specially, for matching file names. If this flag is set, wildcard constructs in pattern cannot match '/' in string. Thus, the only way to match '/' is with an explicit '/' in pattern.

FNM_PERIOD

This flag treats the '.' character specially if it appears at the beginning of string. If this flag is set, wildcard constructs in pattern cannot match ‘.’ as the first character of string.

FNM_NOESCAPE

Don’t treat the ‘\’ character specially in patterns. Normally, ‘\’ quotes the following character, turning off its special meaning (if any) so that it matches only itself. When quoting is enabled, the pattern ‘\?’ matches only the string ‘?’, because the question mark in the pattern acts like an ordinary character.

If you use FNM_NOESCAPE, then ‘\’ is an ordinary character. 

FNM_LEADING_DIR

Ignore a trailing sequence of characters starting with a ‘/’ in string; that is to say, test whether string starts with a directory name that pattern matches.

If this flag is set, either ‘foo*’ or ‘foobar’ as a pattern would match the string ‘foobar/frobozz’.

FNM_CASEFOLD

Ignore case sensitive in comparing string to pattern.

FNM_EXTMATCH

Recognize beside the normal patterns also the extended patterns introduced in ksh. The patterns are written in the form explained in the following table where pattern-list is a | separated list of patterns.

?(pattern-list)
          matches any single character in the pattern-list

*(pattern-list)
          matches everything in the pattern-list

+(pattern-list)
          matches one or more occurrences of  any of the patterns in the pattern-list

@(pattern-list)
          matches exactly one occurrence of the patterns in the pattern-list

!(pattern-list)
          matches if input string is not matches the patter-list

[seq](pattern-list)
          matches the given sequence of the patterns in the pattern-list

[!seq](pattern-list)
          matches if input string is not matches the given sequence of the patterns in the pattern-list


Please write your comments if you find anything is incorrect or do you want to share more information about the topic discussed above.


Logically Proven
Learn, Teach, Share


Ref: GNU C Library

Karthik Byggari

Author & Editor

Computer Science graduate, Techie, Founder of logicallyproven, Love to Share and Read About pprogramming related things.

0 comments:

Post a Comment

 
biz.