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?

···

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

Use "sub", and the non-greedy operator: "?"

"M.raja.gopalan".sub(/(.*?)\./,'')

···

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

Thank you Joel Pearson

···

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

hi Rob Biedenharn

OK, thank you.

···

--
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

···

On 2013-Dec-18, at 04:23 , Joel Pearson <lists@ruby-forum.com> wrote:

Use "sub", and the non-greedy operator: "?"

"M.raja.gopalan".sub(/(.*?)\./,'')

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