Reputation: 192
I want to replace/censor the characters of a username in elixir. So in the end, it should looks like in the bidders list in ebay:
in elixir there is the String replace function, but i am a newbe at regex and i dont know what elixir-regex supports. so how can i achieve this?
iex> String.replace("username1234", someregex, "***")
"u***4"
Upvotes: 0
Views: 234
Reputation: 121000
While you surely might accomplish this with regex “replace all but the first and the last symbols”
String.replace "username1234", ~r/(?<=\A.).*(?=.\z)/, "***"
#⇒ "u***4"
I’d better go with String.slice/3
first = String.slice("username1234", 0, 1)
last = String.slice("username1234", -1, 1)
"#{first}***#{last}"
#⇒ "u***4"
[h | t] = String.graphemes("username1234")
h <> "***" <> List.last(t)
#⇒ "u***4"
Upvotes: 2