Reputation: 54
I'm trying to remove a period prior to the "@"
symbol from an email. I got:
array[0][2].gsub(/\./, '').strip
which removes both periods; "[email protected]"
becomes "anemail@testcom"
, while I'm looking for it to become "[email protected]"
. I can't remove just the single period by itself. What am I doing wrong?
Upvotes: 0
Views: 869
Reputation: 15488
Don't make this more complicated by trying to make it short. Just write it the way you mean it:
a, b = address.split('@')
cleaned = [a.delete('.'), b].join('@')
Upvotes: 0
Reputation: 41838
result = subject.gsub(/\.(?=\S+@)/, '')
Explanation
\.
matches a period(?=\S+@)
lookahead asserts that what follows is any non-whitespace chars followed by an arrobasReference
Upvotes: 0
Reputation: 29124
If there are no periods before @
or if there are more than one period, you can use this regex
email = "[email protected]"
email.gsub(/\.(?=[^@]*\@)/, '')
# => "[email protected]"
Regex explanation: period followed by zero or more occurrence of any character other than @
, followed by an @
If only the first occurrence of a period before @
has to be removed, you can use the same regex with sub
instead of gsub
Upvotes: 1