[erlang-questions] What \& means in regexp:sub/3

kiszl@REDACTED kiszl@REDACTED
Mon Jul 27 09:23:06 CEST 2009


Hi,

The & character in the New string means: put here what RegExp matched from
the string.

> regexp:sub("Hard Day's Night", "^[A-Z][a-z]*", "The &est").
{ok, "The Hardest Day's Night", 1}

Notice how & gets replaced by "Hard" in the result.

Sometimes you might want to actually put the character & in the result. In
this case you need to escape & in New string so it doesn't get replaced:

> regexp:sub("Bonnie and Clyde", "and", "\\&").
{ok, "Bonnie & Clyde", 1}

Regards,
Zoltan.

> Dear All,
>
> In the manual for the regexp,
>
> *sub(String, RegExp, New) -> SubRes
> *
>
> *Types:*
>
> *String = RegExp = New = string()
> SubRes = {ok,NewString,RepCount} | {error,errordesc()}
> RepCount = integer()
> *
>  *Substitutes the first occurrence of a substring matching RegExp in
> Stringwith the string
> New. A & in the string New is replaced by the matched substring of String.
> \& puts a literal & into the replacement string. *
>
> Can someone please explain the sentence "*\& puts a literal & into the
> replacement string. *" . If any example that will be helpful.
>
> Thanks,
> --
> Mathuvathanan Mou.
>




More information about the erlang-questions mailing list