Regular expression

hi,

puts 'raja(dklfjdfldjkfd)gopalan(fdf)'.gsub(/(\(.*\))/, 'raja')

this gives the output "rajaraja"

But I would like to have the output like raja raja gopalan(fdf)

could you please guide me how to write the regular expression for this?

···

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

Google ruby greedy regexp

···

On Dec 23, 2013, at 0:03, Raja gopalan <lists@ruby-forum.com> wrote:

hi,

puts 'raja(dklfjdfldjkfd)gopalan(fdf)'.gsub(/(\(.*\))/, 'raja')

this gives the output "rajaraja"

But I would like to have the output like raja raja gopalan(fdf)

could you please guide me how to write the regular expression for this?

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

This gives the output you said you wanted:

puts 'raja(dklfjdfldjkfd)gopalan(fdf)'.sub(/(\(\w*?\))/, ' raja ')
raja raja gopalan(fdf)

···

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

hi Joel Pearson

Thank you . It works. I designed like

puts 'raja(dklfjdfldjkfd)gopalan(fdf)'.sub(/(\(\.*?\))/, ' raja ')

But it's not working, where is the problem?

···

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

This is a great time to practice your String comparison skills. Your
Regexp is not the same as mine. Spot the difference.

···

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

Ok thank you.

···

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

hi

puts "line1 (SS13864619). line1 (SS13864619), line3
(02).".sub(/(\(\w*?\))(.*)(\(\w*?\))/, '\1\2')

it gives the output "line1 (SS13864619). line1 (SS13864619), line3 ."

But I would like to have the output

line1 (SS13864619). line1 , line3 (02)

could you please guide me how to do that?

···

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

When you're using Regexp to solve a problem, there's usually more than
one input-output scenario, so you can establish a set of rules. If
there's only one scenario then you don't need Regexp.

···

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

Yes you are right, but my problem is, each and every time the numbers
inside the bracket would vary, So I need regular expression for
that,isn't it?

···

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

If that's all that varies:

irb(main):009:0> s.sub(/(\Aline1 \(SS\d+\)\. line1 )(\(SS\d+\))/, '\1')
=> "line1 (SS13864619). line1 , line3 (02)."

···

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

line1 (SS13864619) must have been removed. Output would be,

line1 , line3 (02).

···

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