7stud2
(7stud --)
23 December 2013 10:03
1
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/\ .
7stud2
(7stud --)
23 December 2013 10:59
3
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/ .
7stud2
(7stud --)
23 December 2013 11:17
4
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/ .
7stud2
(7stud --)
23 December 2013 11:38
5
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/ .
7stud2
(7stud --)
26 December 2013 07:31
7
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/ .
7stud2
(7stud --)
26 December 2013 09:32
8
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/ .
7stud2
(7stud --)
26 December 2013 09:42
9
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/ .
7stud2
(7stud --)
26 December 2013 13:10
10
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/ .
7stud2
(7stud --)
26 December 2013 13:22
11
line1 (SS13864619) must have been removed. Output would be,
line1 , line3 (02).
···
--
Posted via http://www.ruby-forum.com/ .