[erlang-questions] escaping values for re:replace

Fred Hebert mononcqc@REDACTED
Tue Jul 27 23:49:09 CEST 2010


Using a fold would be a bit cleaner. I used something like this to write a
quick (and not comprehensive) sluggifying function for French URLs. It
replaces accented characters by their normal counterpart, takes anything
that isn't a character or number and makes it a dash (-), then reduces
multiple dashes to a single one.

sluggify(Str) ->
   Patterns = [{"[âäàáÀÄÂÁ]", "a"},
               {"[éêëèÉÊËÈ]", "e"},
               {"[ïîíìÏÎÌÍ]", "i"},
               {"[öôòóÖÔÒÓ]", "o"},
               {"[üûùúÜÛÙÚ]", "u"},
               {"[ÿýÝ]", "y"},
               {"[çÇ]", "c"},
               {"[^\\w\\d_-]", "-"},
               {"[-]{2,}", "-"},
               {"(?:^-)|(?:-$)", ""}],
   Slug = lists:foldl(fun({Pat, Rep}, Slug) ->
       re:replace(Slug, Pat, Rep, [global, {return, binary}])
   end,
   Str,
   Patterns).

I guess you could use something like that.

On Tue, Jul 27, 2010 at 4:53 PM, Wes James <comptekki@REDACTED> wrote:

> I created a function:
>
> escape(S) ->
>        S2=re:replace(S, "\\*", "\\\\*", [{return,list}, global]),
>        S3=re:replace(S2, "\\[", "\\\\[", [{return,list}, global]),
>        S4=re:replace(S3, "\\(", "\\\\(", [{return,list}, global]),
>        re:replace(S4, "\\)", "\\\\)", [{return,list}, global]).
>
> to escape the values I know. Is there a more erlang way to do this?
> I'm trying to replace all *, [, (, ) with the escaped values.
>
> thx,
>
> -wes
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>


More information about the erlang-questions mailing list