Replace a substring of a string with another string.
Replace a substring of a string with another string.
| Name | Type | Default | Description |
| Return | char* | Pointer to the entry string. The entry string will be changed. There must be sufficient memory available. | |
| str | char* | - | String, in which the replacement(s) are to be made. |
| search | char* | - | Character string to be replaced 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. |
| replace | char* | - | Character string to be entered |
| doAll | int | 1 | 0 : Only replace the first occurrence 1 : Replace all occurrences |
| startPos | int | 0 | Start position for replacings, only valid for regular expressions. |
| 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");
strreplace (str, "tt", "t") returns "Mathias" strreplace (str, "a", "aAa") returns "MaAathiaAas"
Find all 3-digit substrings enclosed by letters and swap this letters.
int main ()
{
char regex [200];
char repl [200];
String str = string::alloc ();
char txt [20000];
// Regular expression
strcpy (regex, "pcre:");
strcat (regex, "([a-z])([1-9]{3})([a-z])");
// Replace
strcpy (repl, "\\3\\2\\1");
// Text
frame::gettext (gFrame, str);
strcpy (txt, string::get (str));
// Real work
strreplace (txt, regex, repl);
frame::replace (gFrame, txt);
return 0;
}
Color all tags of a given tagged text.
int main ()
{
String str = string::alloc ();
char tmp [20000];
char col [500];
int r =255, g = 0, b = 0;
int ro = 8;
int go = 16;
int bo = 24;
textmodel::gettext (str);
strcpy (tmp, string::get (str));
strreplace (tmp, "pcre:(<[a-zA-Z][a-zA-Z0-9_]*\\:(<)?)((([^><])|(\\\\>)|(\\\\<))*)(>)*", "\nccccColorRot\\0ccccColor");
strreplace (tmp, "<", "<0x003C>");
while (strstrpos (tmp, "ccccColorRot") >= 0)
{
sprintf (col, "<cColor:%d, %d, %d><cFont:Verdana,Italic>", r, g, b);
if (r+ro > 255 || r +ro < 0) ro = (-1)*ro;
if (g+go > 255 || g +go < 0) go = (-1)*go;
if (b+bo > 255 || b +bo < 0) bo = (-1)*bo;
r = r + ro;
g = g + go;
b = b + bo;
strreplace (tmp, "ccccColorRot", col, 0);
}
strreplace (tmp, "ccccColor", "<cFont:><cColor:>");
textmodel::replace (tmp);
return 0;
}
Here you will find a InDesign® test document (CS5.5):
Test document
Regular expression working with "pcre:" but not with "regexp:"
int main ()
{
char str [512];
strcpy (str, "abc11188888#defgh11177777#hijklmn11166666#opqäöü55555#qwertzu11144444#XYZ");
wlog ("", "PRE : '%s'\n", str);
strreplace (str, "pcre:([0-9]{6})(#)", "\\2\\1", 1);
wlog ("", "POST : '%s'\n", str);
return 0;
}
Alphabetic index HTML hierarchy of classes or Java