Chapter Contents

Previous

Next
stcpm

stcpm



Unanchored Pattern Match

Portability: SAS/C extension


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <lcstring.h>

int stcpm(char *str, const char *pat, char **substr);


DESCRIPTION

stcpm scans the input string addressed by str for the first occurrence of a substring matching the pattern addressed by pat . The substr function points to a word in which the first match to the search pattern is stored (if a match is found). This pointer is not used if no match is found. (See RETURN VALUE.)

You can specify the search pattern in several ways, possibly including the special characters * , ? , and + , as follows:
* matches zero or more occurrences of the preceding character.
+ matches one or more occurrences of the preceding character.
? matches any character.


RETURN VALUE

stcpm returns the length of the character sequence that matches the search pattern, if successful, or 0 if no match is found. The character pointer that substr addresses points to the first match to the search pattern if a match is found.


CAUTION

The value returned by stcpm ( str , pat , substr ), the length of the character sequence that matches the search pattern, is not necessarily the same as the value returned by strlen(*substr) because strlen(*substr) returns the length of the input string from the beginning of the match to the null character that terminates the string. The length, in this case, may include characters that were not matched.


IMPLEMENTATION

For stcpm , the scan is not anchored. If no match occurs at the first position in the input string str , the next position is checked until a match is found, and so on until the input string is exhausted.


EXAMPLE

#include <lcstring.h>
#include <stdio.h>

main()
{
   static char *sps[ ]  = {
      "----William Tell----",    /* no middle initial         */
      "(((William A Tell)))",    /* middle initial            */
      "...William AAA. Teller",  /* middle initial and period */
      "As William S. Tell Jr"    /* wrong initial             */
   };

   char *reslt;
   char **q;
   int length, i;

   q = &reslt

      /* Find William Tell, whether or not he used his        */
      /*  middle initial.                                     */
   for (i = 0; i < 4; i++) {
      if (length = stcpm(sps[i] , "William A*.* *Tell", q)){
         printf("\n%d. Match result =  ",i);
         fwrite(reslt, 1, length, stdout);
      }
      else
         printf("\n%d. No match for string = %s\n", i, sps[i] );
   }
}


RELATED FUNCTIONS

stcpma , strchr , strstr


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.