Search code examples
regexerlang

How to replace characters in string Erlang?


I have this piece of code that gets sessionid, make it a string, and then create a set with key as e.g. {{1401,873063,143916},<0.16443.0>} in redis. I'm trying replace { characters in this session with letter "a".

OldSessionID= io_lib:format("~p",[OldSession#session.sid]),
StringForOldSessionID = lists:flatten(OldSessionID),
ejabberd_redis:cmd([["SADD", StringForSessionID, StringForUserInfo]]);

I've tried this:

re:replace(N,"{","a",[global,{return,list}]).

Is this a good way of doing this? I read that regexp in Erlang is not a advised way of doing things.


Solution

  • Your solution works, and if you are comfortable with it, you should keep it.

    On my side I prefer list comprehension : [case X of ${ -> $a; _ -> X end || X <- StringForOldSessionID ]. (just because I don't have to check the function documentation :o)