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

Sean Cribbs seancribbs@REDACTED
Wed Jun 24 14:46:01 CEST 2009


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.

If you don't export trim/2 you could choose to remove the guard 
(although I don't recommend it).

Sean

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



More information about the erlang-questions mailing list