Gsub not matching?

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

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

On Tue, Feb 4, 2014 at 2:41 PM, Dave C. [email protected]
wrote:

if question.match(/{ {0,5}([0-9][0-9]? {0,3}- {0,3}[0-9][0-9]?)

PS any suggestions on refactoring the code would be welcome also as I am
learning on my own.

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

Robert K. 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

Dave C. wrote in post #1135501:

Robert K. 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?

I tried the following but only $2 returns anything (40)

question = “A 19 yr old {male, 40%} denies eye trauma but says he
recalls this has happened to him before.”

Manage gender

if (/{\s*(fe)?male\s*,?\s0?(.?(\d+))?\s%?\s*}/) =~ question
x1 = $1
x2 = $2

Dave

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.

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

On Wed, Feb 5, 2014 at 3:56 AM, Dave C. [email protected]
wrote:

Abinoam Jr. wrote in post #1135563:

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!

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 ‘/(?f.+)/=~“foo”;p match’
“foo”
$ ruby -e ‘/f(?.+)/=~“foo”;p match’
“oo”

Kind regards

robert