Append replacement: Difference between revisions
imported>StDoodle Created page with '{{ #vardefine:name|append_replacement}}{{ #vardefine:return_type|buffer}}{{ FunctionPage| name={{#var:name}}| function_category=| function1={{Function| name={{#var:name}}| aggr…' |
imported>Bale m Add paragraph breaks and rephrase for clarity |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{ | {{ | ||
#vardefine:name|append_replacement}}{{ | #vardefine:name|append_replacement(), append_tail}}{{ | ||
#vardefine:return_type|buffer}}{{ | #vardefine:return_type|buffer}}{{ | ||
FunctionPage| | FunctionPage| | ||
name={{#var:name}}| | name={{#var:name}}| | ||
function1={{Function| | function1={{Function| | ||
name= | name=append_replacement| | ||
aggregate={{#var:aggregate}}| | aggregate={{#var:aggregate}}| | ||
return_type={{#var:return_type}}| | return_type={{#var:return_type}}| | ||
return_also={{#var:return_also}}| | return_also={{#var:return_also}}| | ||
parameter1={{Param|matcher| | parameter1={{Param|matcher|match}}| | ||
parameter2={{Param|buffer| | parameter2={{Param|buffer|buff}}| | ||
parameter3={{Param|string|replace}}| | parameter3={{Param|string|replace}}| | ||
p1desc={{Pspan| | }}| | ||
p2desc={{Pspan| | function2={{Function| | ||
p3desc={{Pspan|replace}} is | name=append_tail| | ||
aggregate={{#var:aggregate}}| | |||
return_type={{#var:return_type}}| | |||
return_also={{#var:return_also}}| | |||
parameter1={{Param|matcher|match}}| | |||
parameter2={{Param|buffer|buff}}| | |||
p1desc={{Pspan|match}} is a matcher created with the original string and search pattern| | |||
p2desc={{Pspan|buff}} is the buffer in which the modified string is being accumulated| | |||
p3desc={{Pspan|replace}} is the replacement text for the current match of the search pattern, with the same meanings for special characters as in [[replace_all|replace_all()]]| | |||
}}| | }}| | ||
function_description= | 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. | |||
see_also={{SeeAlso| | </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| | |||
title=Code Samples| | |||
description=This sample will replace every 4-letter word in the input string with its uppercase version.| | |||
code= | |||
<syntaxhighlight> | |||
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? | |||
</syntaxhighlight> | |||
}}| | |||
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?