Search for the first occurrence of the string search in the string str.

Search for the first occurrence of the string search in the string str. The function returns a pointer to the point in the string where the searched character occurs for the first time. If the substring is not found, the call returns 0. To ascertain the first position of a substring in a string use the strchrpos function.

Name Type Default Description
Return char*   Pointer to the first occurrence of the string str in the string str.
0 : String not found
str char* - Character strings which is to be searched
search char* - Search for this substring. Must not be equal to "".

since v3.2.3 R2600, 08.08.2011 The search string may be a regular expression. Use prefix "regexp:" to mark a search string as a regular expression.

since v3.3 R4000, 4. Jul 2013 With prefix "pcre:" a new machine (the so called 'perl compatible regular expressions) for regular expressions is used. We recomment using this machine!

Since v3.4 R9000 and CS5 only PCRE is used for regulare expressions. The prefix regexp: is still allowed but will use PCRE too. Only CS4 still uses the old GNU compatible implementation.

startPos int 0 Start index to search
matchlen int* 0 (for regular expressions only) Length of found sub string
matches int* 0 (for regular expressions only) int-Array containing pairs of positions and lengths for the sub-entries of the next match.

The first pair gives the position and the length of whole expression. The following pairs containing the positions and lengths of every sub-expression. Sub-expressions of an regular expressions are every "(...)" substring of the regular expression, for instance ([a-z]{3,7}). The array is closed by the pair (-1, 0).

Attention : Sub entries may have a length of 0! The regular expression ([0-9])([a-z]{0,7})([0-9])) applied to "14" will return an empty string for the second sub-expression.

Take care to allocate enough space, if you are using this array. You need at least (1 + number of sub-expressions + 1) * 2 integers.

Simple way to define such an array for a regular expression with 4 sub-entries.

int matches [6];
compile_options int 0 Additional options while compiling regular expression, see here
study_options int 0 Additional options while studying compiled regular expression, see here
exec_options int 0 Additional options while executing regular expression, see here
strcpy (str, "Matthias");
strstr (str, "hi") returns "hias"

And here an examples using regular expressions:

int main ()
{
    char			str [512];
    int				pos		= 0;
    char		*	sub;
    int				matches	[2 * (1 + 2 +1)];	// 2 * (1 +number of sub-expressions + 1)
    int				i, p, l;
strcpy (str, "abc11188888#defgh11177777#hijklmn11166666#opqäöü11155555#qwertzu11144444#XYZ");
while (1) { sub = strstr (str, "pcre:([0-9]{6})(#)", pos, 0, matches); if (!sub) break;
wlog ("", "Pos %d : '%s'\n", pos, sub);
i = 0; while (1) { p = matches [i*2]; // startpos of sub token l = matches [i*2+1]; // length of sub token if (p < 0) break;
wlog ("", " %d. [%d, %d] : '%s'\n", i, p, l, strsubstring (str, p, l)); ++i; }
pos = matches [0] + matches [1] + 1; }
return 0; }

Since
Version 1.2.1 (12 September 2005)
Regular expressions since v3.2.3 R2600, 08.08.20112005)
Regular expressions with "pcre:" since v3.3.1 R4000, 4. Jul 2013 compile_options, study_options, exec_options since v4.0.5 R20456, 10. Oct 2017

Available
priint:comet InDesign® Plug-Ins, comet_pdf, Illustrator

See Also
strchrpos
strstr
strstrpos

Alphabetic index HTML hierarchy of classes or Java