regrep |
Discworld creator help |
regrep |
Name
regrep - A fancy way of doing string replacement.
Syntax
string regrep( string str, string *pattern, mixed *replacement );
Parameters
str - the string to replace in
pattern - regexp bits to match
replacement - bits to replace with
Description: the 1st & 2nd arguments
regrep() is used to do more advanced replacement than replace() can do.
Note: It is much easier to understand regrep() if you have read and thought through the helpfile for regexp.
string regrep( string str, string *pattern, mixed *replacement );
Things are replaced in the string str.
The second argument is a series of regular expressions that together make the text to replace; i.e. their matches make up a new string. This new string is searched for in str. Each found match will be replaced with the third argument.
Description: the 3rd argument
Each element of the third argument can be either a string, int or an array.
A string just gets copied over from the replacement variable to the str variable (i.e. the value of the third argument replaces the matched piece in str).
Ints are replaced by what matched the <number>th element in the matching array (starting from 1). I.e. number = 1 -> first matched bit will be replaced with number.
If the third argument is an array, it will be of the format ({ (: function :), number }). The function will be called with a parameter, which is the <number>th matched bit. The function's result will replace the matched bit.
To play around with this you could use the following exec and change things and see what happens. I recommend starting out with using the exec unaltered at first.
exec string str = "frog1 frog2"; str = regrep( str, ({ "[..o.]" }), ({ "0" }) ); return str;
Example 1
Say we want to replace every occurrence of "bing" followed by a number with "frog" followed by the same number (e.g. replace every occurrence of "bing2" with "frog2" or "bing9" with "frog9"). We would do:
result = regrep( str, ({ "bing", "[0-9]+" }), ({ "frog", 2 }) );
The second argument, the pattern array, consists of "bing", which matches the string "bing", followed "[0-9]+", which is a regexp matching any number. Those two is what we want to match.
Each bit of the string that is matched by this is replaced with the replacement array, which consists of first "frog", a simple string, and then 2, which will replaced with the second match, that is "[0-9]+", which is the number matched.
So, if passed the string "bing744 bing frog3 bing4ever frogfluff", "bing744" and "bing4" would be matched, and replaced with "frog744" and "frog4", resulting in "frog744 bing frog3 frog4ever frogfluff"
Example 2
If you'd want to replace all words in a string str followed by a number with 'bing':
str = "fsdfsdf3432 bing 12 sge4 etc"
result = regrep( str, ({ "[a-zA-Z]+", "[0-9]+" }), ({ "bing" }));
result would be "bing bing 12 bing etc"
Example 3
If you want to replace "bing <string> wibble" with "bing wibble <string>": Note: <string> as shown above will be 'frog' in this example.
result =
regrep( "stuff bing frog wibble",
({ "bing ", "[a-zA-Z]+", " wibble" }), ({ "bing wibble ", 2 }) );
result = "stuff bing wibble frog"
Example 4
If you want to add 1 to all numbers after bing, you can do:
int addone( string text ) {
int num;
sscanf( text, "%d", num );
return num + 1;
}
result = regrep( "bing1 wibble2 frog3 bing4",
({ "bing", "[0-9]" }), ({"bing", ({ (: addone :), 2 }) }) );
result = "bing2 wibble2 frog3 bing5"