blob: 912e864dc54de2b4b74fa05704ede7ba475f094e [file] [log] [blame]
Zesstra715ec202025-07-09 22:18:31 +02001SYNOPSIS
2 #include <regexp.h>
3
4 string regreplace(string txt, string pattern,
5 closure|string replacepattern, int flags)
6
7DESCRIPTION
8 This function looks through txt looking for the regular
9 expression pattern. If it finds it, it replaces it by the
10 replacepattern.
11
12 The replacepattern can be a constant string, or a closure taking
13 the matched substring and the position at which it was found
14 as arguments and returning the replacement pattern string.
15
16 The flag is a bitmask of the usual regexp options. Additionally
17 the efun recognizes RE_GLOBAL: if set, the search and replace
18 is repeated as often as the pattern matches.
19
20 The function returns the modified string (or the original if it
21 wasn't modified).
22
23 The function behaves like the s/pattern/replacepattern/flags
24 in editors as ed/vi or sed. The power of this function lies in
25 replacing variable strings (as opposed to regexplode, where
26 you can explode by regular expression, but not implode...)
27
28
29EXAMPLES
30 string msgin;
31
32 /* Checks msgin for the string 'tells you: ' and all following
33 * characters and encloses those characters by <underline>
34 * and </underline>. global.
35 */
36 msgin = regreplace(msgin, "tells you: (.*)",
37 "tells you: <underline>\\1</underline>", 1);
38
39 /* replaces all <underline> html tags by the vt100 escape
40 * sequence for underline.
41 */
42 txt = regreplace(txt, "<underline>", "<ESC>[5m", 1);
43
44 /* Put the word HOUSE into lower case. */
45 txt = regreplace(txt, "HOUSE",
46 function string (string s) {return lower_case(s);},
47 1);
48
49
50HISTORY
51 Introduced in 3.2.1@125.
52 The use of a closure as replacepattern was introduced in
53 LDMud 3.2.9.
54
55AUTHOR
56 Marcus@TAPPMud contributed the original idea for the efun and the
57 man page.
58
59SEE ALSO
60 regexp(E), regexplode(E), regmatch(E), regexp_package(E),
61 sscanf(E), trim(E), regexp(C)