0
votes

I'm using Regexes (Regices?) for the first time in Elixir and came upon two functions which, at first glance, seem identical, save for the ordering of their arguments: String.replace/4 and Regex.replace/4.

Both of them take a string, a pattern, a replacement, and a list of options, and return the replaced string.

Is there any difference between the two? In what case should I use one over the other?

2
You can clearly see the usage difference from the docs. The goal of these functions is the same. Why ask? What is your specific problem? - Wiktor Stribiżew
"In what case should I use one over the other?" is my specific problem. - Niek
It is up to you if you want to use a regex, what is your input? What is the expected output? - Wiktor Stribiżew
This is not a question about how to use regexes, but a question about the language design of elixir and the difference between two implementations of the same functionality. I have, upon reflection, removed the 'regex' tag from the question, as I no longer deem it appropriate. - Niek
I answered to this question here: gist.github.com/am-kantox/bfa82ff705ca448efd93c30e08a9631d I am positive the question is legit and has a concrete answer. - Aleksei Matiushkin

2 Answers

1
votes

String.replace/4 provides additional functionality over Regex.replace/4. It's equivalent to Regex.replace/4 in the specific situation when:

  • The pattern argument is a %Regex{} struct.
  • the replacement argument is a binary or a single-arity function.

You can see String.replace/4 calling Regex.replace/4 in this situation in the source code.

For regular expressions, it doesn't matter which one you use.

However, there are many ways to use String.replace/4 without a regular expression. They are listed in the documentation, so I don't think it's necessary to repeat them here. It could be a good idea to state what you are actually trying to do, because it may be that you have an XY problem and that in Elixir using a regular expression isn't the best tool for the task you are undertaking.

-2
votes

Yes, Both of them take string, a pattern and replacement. The difference is, String will replace the string with "replacement string" if the pattern string exist in the input string. Ex: String.replace("abc", "a", "A") - this will result in Abc. It will look for the search string "a" and replace the string with "A".

Whereas in Regex.Replace - here the search pattern will be considered as regular expression unlike string.

Ex: Regex.replace(~r/b/, "abc", "d") - This will result in "adc" . Here the pattern "b" will be considered as regular expression.

To make you clear, Regex.replace(~r/\d/ "A2B3C", "-"), this will result in A-B-C. Here the search pattern is \d - which mean any number. Regex will replace the numbers with "-" in the given string. Where as if you use the same search pattern for string String.replace("A2B3C", "d", "-") - this will result in "A2B3C". Nothing gets replaced.

Look at https://www.regular-expressions.info for more information about the regex patterns