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 the position in the string where the substring begins. If the substring is not found, the call returns -1.

Name Type Default Description
Return int   0-based position in the string in which the substring begins
-1 : Character not found
str char* - Character string 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 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 (for regular expressions only) Start search at this character (not byte!) position
matchlen int* 0 (for regular expressions only) Length in characters of the found result
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");
strstrpos (str, "hi") returns 4

Colorize all words of the given text. Take care to map all positions and lengths to characters!

int get_color (int * i, int * r, int * g, int * b)
{
    if (*i > 8)		*i = 0;
if (*i == 0) {*r = 100; *g = 149; *b = 23; } if (*i == 1) {*r = 210; *g = 105; *b = 30; } if (*i == 2) {*r = 255; *g = 99; *b = 71; } if (*i == 3) {*r = 255; *g = 215; *b = 0; } if (*i == 4) {*r = 216; *g = 191; *b = 216; } if (*i == 5) {*r = 175; *g = 238; *b = 238; } if (*i == 6) {*r = 173; *g = 255; *b = 47; } if (*i == 7) {*r = 0; *g = 191; *b = 255; } if (*i == 8) {*r = 218; *g = 165; *b =32; }
*i = *i + 1; return 1; }
int main () { String str = string::alloc (); char regex [20000]; char tmp [20000]; char sub [20000]; int startPos = 0; int matchlen; int matches [400]; int i, m = 0, cc = 0; int r, g, b; int slen;
strcpy (regex, "pcre:[^[:space:][:punct:]//.]+");
frame::gettext (gFrame, str); strcpy (tmp, string::get (str));
slen = strlen (tmp); while (1) { startPos = strstrpos (tmp, regex, startPos, &matchlen, matches); if (startPos < 0) break; m = m+1;
i = 0; while (matches[i*2] >= 0 && matches[(i*2)+1] >= 0) { strcpy (sub, tmp, matches[i*2], matches[i*2+1]); wlog ("", "Token %d/%d : [%d, %d]\n", m, i, matches[i*2], matches[i*2]+matches[i*2+1]); wlog ("", " '%s'\n", sub);
get_color (&cc, &r, &g, &b); textmodel::set_color_rgb (   frameB,   matches[i*2],   matches[i*2+1],   r, g, b); i = i + 1; }
if (matchlen == 0) startPos = startPos +1; else startPos = startPos + matchlen; }
return 0; }

Here you will find a InDesign® test document (CS5.5):

Test document

This example will not work with "regexp:" but only with "pcre:".

int main ()
{
    char		str [512];
    int			pos		= 0;
    int			matchlen;
strcpy (str, "abc11188888#defgh11177777#hijklmn11166666#opqäöü11155555#qwertzu11144444#XYZ");
while (1) { pos = strstrpos (str, "pcre:([0-9]{6})(#)", pos, &matchlen); if (pos<0) break;
wlog ("", "[%d, %d] : '%s'\n", pos, matchlen, strsubstring (str, pos, matchlen));
pos += matchlen + 1; }
return 0; }

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

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

See Also
strchr
strstr
strstrpos

Alphabetic index HTML hierarchy of classes or Java