Regular expression,

hi

“M.raja.gopalan”.gsub(/(.*)./,’’)

It gives gopalan as output, but I need to replace until first “.”
(i.e)output would be raja.gopalan.

How would I do this?

Use “sub”, and the non-greedy operator: “?”

“M.raja.gopalan”.sub(/(.*?)./,’’)

Thank you Joel P.

hi Rob B.

OK, thank you.

On 2013-Dec-18, at 04:23 , Joel P. [email protected] wrote:

Use “sub”, and the non-greedy operator: “?”

“M.raja.gopalan”.sub(/(.*?)./,’’)


Posted via http://www.ruby-forum.com/.

Alternatively, you could match all non-. characters up to the first .
using:

“M.raja.gopalan”.sub(/\A[^.]*./,’’)

\A anchors the match to the beginning of the string
[^.] matches any character that isn’t a .

irb2.0.0> “M.raja.gopalan”.sub(/\A[^.]*./,’’)
#2.0.0 => “raja.gopalan”

-Rob

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs