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.

Simple illustration of how the method works

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_with inside 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 parameter to_be_replaced.

  • max_occurrence: This is an integer value, which indicates the maximum occurrence of the parameter to_be_replaced. If this parameter is 0 or less than zero, all occurrences of to_be_replaced in the parameter sequence_to_replace_in will be exchanged.

Return value

This function returns a sequence, which is the manipulated sequence.

Note: To use this function, we must include the search.e file in our program code:

include std/search.e
include std/search.e
--declare some variables
sequence to_be_replaced, sequence_to_replace_in, replace_with
sequence outcome
atom max_occurrence = 0
--assign values to variables
to_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 function
outcome = match_replace(to_be_replaced,sequence_to_replace_in,replace_with,max_occurrence)
--display output to screen
puts(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.

Free Resources