Append replacement: Difference between revisions
imported>Jasonharper Explainified! |
imported>Bale m Add paragraph breaks and rephrase for clarity |
||
(One intermediate revision by the same user not shown) | |||
Line 5: | Line 5: | ||
FunctionPage| | FunctionPage| | ||
name={{#var:name}}| | name={{#var:name}}| | ||
function1={{Function| | function1={{Function| | ||
Line 28: | Line 27: | ||
}}| | }}| | ||
function_description=These two functions are the building blocks for a regular expression-based search and replace operation. Most of the time, you can use the more straightforward [[replace_string|replace_string()]] or [[replace_all|replace_all()]] functions instead; the only time you actually need to use these functions is when the replacement text has to be generated in some special way, rather than being a constant. | function_description=These two functions are the building blocks for a regular expression-based search and replace operation. Most of the time, you can use the more straightforward [[replace_string|replace_string()]] or [[replace_all|replace_all()]] functions instead; the only time you actually need to use these functions is when the replacement text has to be generated in some special way, rather than being a constant. Both functions return {{Pspan|buff}}, for convenience. | ||
To use these functions, you must have a matcher variable which has been created with the desired search pattern and original text, and a buffer variable in which the modified string will be built. append_replacement() | To use these functions, you must have a matcher variable which has been created with the desired search pattern and original text, and a buffer variable in which the modified string will be built. | ||
</p><p>'''append_replacement()''' is only valid immediately after [[find|find()]] has returned true for {{Pspan|match}}. It will copy text into the buffer starting after the previous match (or the start of the original string) through the start of the current match, and will then append the {{Pspan|replace}} string. | |||
</p><p>'''append_tail()''' is normally called only once, after find() returns false. It will copy any remaining text into the buffer, after the final match.| | |||
code1={{CodeSample| | code1={{CodeSample| | ||
Line 55: | Line 58: | ||
see_also={{SeeAlso|create_matcher}}| | see_also={{SeeAlso|create_matcher}}| | ||
}} | }} | ||
[[Category:String Handling Routines]] |
Latest revision as of 19:50, 12 August 2015
Function Syntax
buffer append_replacement(matcher match ,buffer buff ,string replace )
buffer append_tail(matcher match ,buffer buff )
- match is a matcher created with the original string and search pattern
- buff is the buffer in which the modified string is being accumulated
- replace is the replacement text for the current match of the search pattern, with the same meanings for special characters as in replace_all()
These two functions are the building blocks for a regular expression-based search and replace operation. Most of the time, you can use the more straightforward replace_string() or replace_all() functions instead; the only time you actually need to use these functions is when the replacement text has to be generated in some special way, rather than being a constant. Both functions return buff, for convenience. To use these functions, you must have a matcher variable which has been created with the desired search pattern and original text, and a buffer variable in which the modified string will be built.
append_replacement() is only valid immediately after find() has returned true for match. It will copy text into the buffer starting after the previous match (or the start of the original string) through the start of the current match, and will then append the replace string.
append_tail() is normally called only once, after find() returns false. It will copy any remaining text into the buffer, after the final match.
Code Samples
This sample will replace every 4-letter word in the input string with its uppercase version.
string cap4(string input)
{
buffer b;
matcher m = create_matcher("\\b\\w{4}\\b", input);
while (m.find()) {
m.append_replacement(b, m.group().to_upper_case());
}
m.append_tail(b);
return b.to_string();
}
print(cap4("did this script work right?"));
// Output will be: did THIS script WORK right?