Replace first: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Bale
Created page with '{{ #vardefine:name|replace_first}}{{ #vardefine:return_type|string}}{{ FunctionPage| name={{#var:name}}| function_category=String Handling Routines| function1={{Function| name=…'
 
imported>Heeheehee
m Minor fix to code sample.
 
(One intermediate revision by one other user not shown)
Line 5: Line 5:
FunctionPage|
FunctionPage|
name={{#var:name}}|
name={{#var:name}}|
function_category=String Handling Routines|


function1={{Function|
function1={{Function|
Line 27: Line 26:
string input = "zzzdogzzzdogzzz";
string input = "zzzdogzzzdogzzz";
matcher test_match = create_matcher("dog", input);
matcher test_match = create_matcher("dog", input);
string result = replace_all(test_match, "cat");
string result = replace_first(test_match, "cat");
print(result);
print(result);
</syntaxhighlight>|
</syntaxhighlight>|
Line 36: Line 35:
{{SeeAlso|replace_all}}|
{{SeeAlso|replace_all}}|
}}
}}
[[Category:String Handling Routines]]

Latest revision as of 20:34, 5 August 2010

Function Syntax

string replace_first(matcher pattern ,string replacement )

  • pattern is the regular expression to use
  • replacement is used to replace a match of the pattern

This function first resets the matcher pattern. It then scans the input sequence looking a match of the pattern. Characters that are not part of any match are appended directly to the result string with the first match replaced by the replacement string.

Simple Example

For example:

string input = "zzzdogzzzdogzzz";
matcher test_match = create_matcher("dog", input);
string result = replace_first(test_match, "cat");
print(result);

Will result in the following being printed:

zzzcatzzzdogzzz


See Also

replace_all()