D
D
dexdev2014-10-07 17:09:25
ruby
dexdev, 2014-10-07 17:09:25

How to correctly write a regular expression replacing the exact number of characters?

There is a regular expression that replaces everything before @ with [email protected] I would like it to be replaced not just with "[email protected]" but with the exact number of characters, tell me how to do it right about the great ones !!!

def protected_email
    self.email.gsub /[^@]*@/, '[email protected]'
  end

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victor Maslov, 2014-10-07
@AdilA

.gsub(/[^@](?=.*[email protected])/, ?x)

V
vsuhachev, 2014-10-07
@vsuhachev

Open the documentation for the Regexp class, read distributed Repetition

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question