What is the match_replace() method in Euphoria?
Overview
Scripting languages such as javascript and PHP allow a lot of flexibility while working with arrays. This is because these languages have built-in methods to manipulate an array. In Euphoria, the sequence is used as an array of integers or strings. One such manipulation carried out on a sequence is searching.
In this shot, we’ll learn a sequence search method called match_replace().
What is the search_replace() method?
The search_replace() is a built-in method of the search.e file in the standard library. It performs a search on a sequence and replaces the character that has been indicated with another character.
Syntax
match_replace(to_be_replaced, sequence_to_replace_in,replace_with,max_occurrence)
Parameters
All the parameters are mandatory.
-
to_be_replaced: This parameter indicates the substring, which we wish to replace with another:replace_withinside the sequence. -
sequence_to_replace_in: This is the sequence where the substring,to_be_replaced, could be found and replaced. -
replace_with: This is the substring with which we want to exchange the parameterto_be_replaced. -
max_occurrence: This is an integer value, which indicates the maximum occurrence of the parameterto_be_replaced. If this parameter is0or less than zero, all occurrences ofto_be_replacedin the parametersequence_to_replace_inwill be exchanged.
Return value
This function returns a sequence, which is the manipulated sequence.
Note: To use this function, we must include the
search.efile in our program code:
include std/search.e
include std/search.e--declare some variablessequence to_be_replaced, sequence_to_replace_in, replace_withsequence outcomeatom max_occurrence = 0--assign values to variablesto_be_replaced = "try"sequence_to_replace_in = "try, if you fall try again, just don't stop to try"replace_with = "rise"--use the replace functionoutcome = match_replace(to_be_replaced,sequence_to_replace_in,replace_with,max_occurrence)--display output to screenputs(1,match_replace("Flash", "Flash the Flash Light \n","FLASH",1))printf(1, "%s", {outcome})
Explanation
- Line 4–6: We declare some variables.
- Line 9–11: We assign values to the declared variables.
- Line 14: We call the
match_replace()function and provide the four required parameters. - Line 17 and 18: We print the output of the function.