Cldr.Number.Parser.find_and_replace
find_and_replace
, go back to Cldr.Number.Parser module for more information.
Specs
find_and_replace(%{required(binary()) => term()}, binary(), float() | nil) :: {:ok, list()} | {:error, {module(), binary()}}
Find a substring at the beginning and/or end of a string, and replace it.
Ignore any whitespace found at the start or end of the string when looking for a match. A match is considered only if there is no alphabetic character adjacent to the match.
When multiple matches are found, the longest match is replaced.
Arguments
string_map
is a map where the keys are the strings to be matched and the values are the replacement.string
is the string in which the find and replace operation takes place.fuzzy
is floating point number between 0.0 and 1.0 that is used to implement a fuzzy match usingString.jaro_distance/2
. The default isnil
which means the match is exact at the beginning and/or the end of thestring
.
Returns
{:ok, list}
where list isstring
broken into the replacement(s) and the remainder after find and replace. Or{:error, {exception, reason}}
will be returned if thefuzzy
parameter is invalid or if no search was found and no replacement made. In the later case,exception
will beCldr.Number.ParseError
.
Examples
iex> Cldr.Number.Parser.find_and_replace(%{"this" => "that"}, "This is a string")
{:ok, ["that", " is a string"]}
iex> Cldr.Number.Parser.find_and_replace(%{"string" => "term"}, "This is a string")
{:ok, ["This is a ", "term"]}
iex> Cldr.Number.Parser.find_and_replace(%{"string" => "term", "this" => "that"}, "This is a string")
{:ok, ["that", " is a ", "term"]}
iex> Cldr.Number.Parser.find_and_replace(%{"unknown" => "term"}, "This is a string")
{:error, {Cldr.Number.ParseError, "No match was found"}}