[Fwd: Re: [erlang-questions] Smart way to trim a binary?]

Per Melin per.melin@REDACTED
Wed Jun 24 15:35:43 CEST 2009


Sean Cribbs:
> Why not use the re module? It works the same on binaries and strings.
>
> -define(TRIM_REGEXP, "^\\s*(.*)\\s*$").
>
> trim(Bin) when is_binary(Bin) -> trim(Bin, binary);
> trim(List) when is_list(List) -> trim(Bin, list).
>
> trim(Subject, Type) when list =:= Type orelse binary =:= Type ->
> {match, [Trimmed]} = re:run(Subject, ?TRIM_REGEXP, [{capture, [1], Type}]),
> Trimmed.

Another way:
re:replace(Subject, "^\\s+|\\s+$", "", [global])

> p.s. Reply-To bit me again. Pretty please with sugar on top, will someone
> fix that?

+1 on that, but don't hold your breath. I think the people in charge
have deemed the current behaviour a feature.


More information about the erlang-questions mailing list