7stud2
(7stud --)
4 February 2014 13:41
1
Hi,
Can anyone see an obvious reason why
question.gsub!(/\{#{number_range}\}/, new_num) is not finding the
existing match in the question?
question = "A {30-45} year old {asthmatic, diabetic, hypertensive}
{male, .4} presents with blurry vision in the {left} eye. ... "
# Manage number range
if question.match(/\{ {0,5}([0-9][0-9]? {0,3}- {0,3}[0-9][0-9]?)
{0,5}\}/)
number_range = question.match(/\{ {0,5}([0-9][0-9]? {0,3}-
{0,3}[0-9][0-9]?) {0,5}\}/)
x1 = question.match(/\{ {0,5}([0-9][0-9]? {0,3}- {0,3}[0-9][0-9]?)
{0,5}\}/).to_s[1..-2].split('-')
new_num = (rand(x1[0].to_i..x1[1].to_i)).to_s
question.gsub!(/\{#{number_range}\}/, new_num)
end
when I "puts number_range" I get {30-45} so it should find the match.
PS any suggestions on refactoring the code would be welcome also as I am
learning on my own.
Thanks
Dave
···
--
Posted via http://www.ruby-forum.com/ .
I think it's the input, my value for number_range does not have the {...}:
question = "A {30-45} year old {asthmatic, diabetic, hypertensive} {male,
.4} presents with blurry vision in the {left} eye. ... "
number_range = '30-45'
new_num = '42'
puts question.gsub!(/\{#{number_range}\}/, new_num) => A 42 year old
{asthmatic, diabetic, hypertensive} {male, .4} presents with blurry vision
in the {left} eye. ...
Robert_K1
(Robert K.)
4 February 2014 14:32
3
How about:
question = "A {30-45} year old {asthmatic, diabetic, hypertensive}
{male, .4} presents with blurry vision in the {left} eye. ... "
if /\{\s*(\d+)\s*-\s*(\d+)\s*\}/ =~ question
n1 = Integer($1)
n2 = Integer($2)
question[/\{\s*(\d+)\s*-\s*(\d+)\s*\}/] = "{#{rand(n1..n2)}}"
puts question
end
or even
question = "A {30-45} year old {asthmatic, diabetic, hypertensive}
{male, .4} presents with blurry vision in the {left} eye. ... "
if /\{\s*(\d+)\s*-\s*(\d+)\s*\}/ =~ question
n1 = Integer($1)
n2 = Integer($2)
question[/\{(\s*\d+\s*-\s*\d+\s*)\}/, 1] = rand(n1..n2).to_s
puts question
end
Cheers
robert
···
On Tue, Feb 4, 2014 at 2:41 PM, Dave Castellano <lists@ruby-forum.com> wrote:
Hi,
Can anyone see an obvious reason why
question.gsub!(/\{#{number_range}\}/, new_num) is not finding the
existing match in the question?
question = "A {30-45} year old {asthmatic, diabetic, hypertensive}
{male, .4} presents with blurry vision in the {left} eye. ... "
# Manage number range
if question.match(/\{ {0,5}([0-9][0-9]? {0,3}- {0,3}[0-9][0-9]?)
{0,5}\}/)
number_range = question.match(/\{ {0,5}([0-9][0-9]? {0,3}-
{0,3}[0-9][0-9]?) {0,5}\}/)
x1 = question.match(/\{ {0,5}([0-9][0-9]? {0,3}- {0,3}[0-9][0-9]?)
{0,5}\}/).to_s[1..-2].split('-')
new_num = (rand(x1[0].to_i..x1[1].to_i)).to_s
question.gsub!(/\{#{number_range}\}/, new_num)
end
when I "puts number_range" I get {30-45} so it should find the match.
PS any suggestions on refactoring the code would be welcome also as I am
learning on my own.
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
7stud2
(7stud --)
4 February 2014 16:18
4
Robert Klemme wrote in post #1135495:
or even
question = "A {30-45} year old {asthmatic, diabetic, hypertensive}
{male, .4} presents with blurry vision in the {left} eye. ... "
if /\{\s*(\d+)\s*-\s*(\d+)\s*\}/ =~ question
n1 = Integer($1)
n2 = Integer($2)
question[/\{(\s*\d+\s*-\s*\d+\s*)\}/, 1] = rand(n1..n2).to_s
puts question
end
Cheers
robert
Thank you! You have given me lots to look up and learn about from your
examples.
Dave
···
--
Posted via http://www.ruby-forum.com/\ .
7stud2
(7stud --)
5 February 2014 00:30
5
Dave Castellano wrote in post #1135501:
Robert Klemme wrote in post #1135495:
if /\{\s*(\d+)\s*-\s*(\d+)\s*\}/ =~ question
n1 = Integer($1)
n2 = Integer($2)
Can you point me in the direction to look in order to understand the $1 and $2.
Are they variables inherent to regex?
···
Dave
--
Posted via http://www.ruby-forum.com/\ .
abinoam
(Abinoam Praxedes Marques Jr.)
5 February 2014 02:43
6
Dear Dave,
Pattern matching sets some global variables :
$~ is equivalent to ::last_match;
$& contains the complete matched text;
$` contains string before match;
$' contains string after match;
$1, $2 and so on contain text matching first, second, etc capture group;
$+ contains last capture group.
Abinoam Jr.
···
From: Class: Regexp (Ruby 2.1.0)
On Tue, Feb 4, 2014 at 9:30 PM, Dave Castellano <lists@ruby-forum.com> wrote:
Dave Castellano wrote in post #1135501:
Robert Klemme wrote in post #1135495:
if /\{\s*(\d+)\s*-\s*(\d+)\s*\}/ =~ question
n1 = Integer($1)
n2 = Integer($2)
Can you point me in the direction to look in order to understand the $1 and $2.
Are they variables inherent to regex?
Dave
--
Posted via http://www.ruby-forum.com/\ .
7stud2
(7stud --)
5 February 2014 02:56
7
Abinoam Jr. wrote in post #1135563:
Dear Dave,
From:
Pattern matching sets some global variables :
$~ is equivalent to ::last_match;
$& contains the complete matched text;
$` contains string before match;
$' contains string after match;
$1, $2 and so on contain text matching first, second, etc capture group;
$+ contains last capture group.
Abinoam Jr.
Thank you - thats a huge help!
Dave
···
--
Posted via http://www.ruby-forum.com/\ .
Robert_K1
(Robert K.)
5 February 2014 09:48
8
There is one important thing you should know: these global variables
are not really global. They are per thread and per scope.
$ ruby -e '/fo+/ =~ "foo"; p $~;Thread.new {p $~}.join'
#<MatchData "foo">
nil
$ ruby -e 'def m(s) /f.+/ =~ s; p $~ end;/f.+/ =~ "foo"; p $~;m("faa");p $~'
#<MatchData "foo">
#<MatchData "faa">
#<MatchData "foo">
$ ruby -e '/f.+/=~"foo";p $~;1.times {/f.+/=~"faa";p $~};p $~'
#<MatchData "foo">
#<MatchData "faa">
#<MatchData "faa">
This is convenient as it avoids multiple matches to interfere with each other.
Btw. you can also make the regex fill local variables:
$ ruby -e '/(?<match>f.+)/=~"foo";p match'
"foo"
$ ruby -e '/f(?<match>.+)/=~"foo";p match'
"oo"
Kind regards
robert
···
On Wed, Feb 5, 2014 at 3:56 AM, Dave Castellano <lists@ruby-forum.com> wrote:
Abinoam Jr. wrote in post #1135563:
Class: Regexp (Ruby 2.1.0)
Pattern matching sets some global variables :
$~ is equivalent to ::last_match;
$& contains the complete matched text;
$` contains string before match;
$' contains string after match;
$1, $2 and so on contain text matching first, second, etc capture group;
Thank you - thats a huge help!
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/