Chapter Contents |
Previous |
Next |
stcpm |
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. |
William A. Tell
as follows:
stcpm(str, "William A. Tell", substr);
?
) for the elements that can vary:
stcpm(str, "William ?. Tell", substr);
*
) in the search pattern.
The asterisk should follow the character that may occur several times or
not occur at all:
stcpm(str, "William A*. Tell", substr);
This statement matches the following list with any number of occurrences of A:
William . Tell William A. Tell William AAAAA. Tell
Similarly, this statement matches the list items that follow it:
stcpm(str, "William A*.* *Tell", substr);
(" ")
; a blank and one A
(" A")
; or a blank and several A's
(" AAA")
:
William . Tell William A. Tell William AAAAA. Tell
William A Tell William A. Tell William ..... Tell
William A.Tell William A. Tell William A. Tell
William AAATell William ... Tell William Tell
stcpm(str, "William ?*.* *Tell", substr);
For an exact match with William ?. Tell, use this statement:
stcpm(str, "William \?. Tell", substr);
By comparison, no match is found for William A. Tell or for William . Tell when you use the search pattern in this statement.
+
) following the character
in the search pattern:
stcpm(str, "William A. T+e+l+l+", substr);
The search pattern in the above statement matches the following:
William A. Tell William A. TTTeeelll
The matching continues with multiple occurrences
of T, e, and l. Note that the plus sign matches one or more occurrences,
but not zero occurrences of the character. (To find a match where there may
be no occurrence of a character, use an asterisk (
*
) following the character.)
+
) or asterisk (
*
), precede the
+
or
*
symbol
in the search pattern with a backslash.
You can combine the
+
,
*
, and
?
symbols
in the search pattern when various combinations of characters are to be matched.
Consider these examples:
/* ?+ matches one or more question marks */ char *pat1 = "William \\?+.* *Tell"; /* ?+ matches zero or more question marks */ char *pat2 = "William \\?*.* *Tell";
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 |
SEE ALSO |
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.