# Value of $1is retained

**URL:** <https://www.ruby-forum.com/t/value-of-1is-retained/101844>\
**Category:** Ruby\
**Created:** [August 8, 2006, 10:32am UTC](https://www.ruby-forum.com/t/value-of-1is-retained/101844 "2006-08-08T10:32:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anders\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@Anders\_V](https://www.ruby-forum.com/u/Anders_V)\
**Post date:** [August 8, 2006, 10:32am UTC](https://www.ruby-forum.com/t/value-of-1is-retained/101844/1 "2006-08-08T10:32:09Z")

</div>

Hi  
I am using regexp in a loop to extract a value from a  
string. I have noticed that when string is nil $1 keeps its  
old value, which means that the expression after regexp  
does not get the correct value (which should be nil). The  
code looks like this

(0…key\_colnames.length-1).each do |i|  
person\_data[key\_colnames[i]] =~ /"?([\w-:]\*)"?/

# assigning $1 to something

end

Shouldn’t the $-variables be reset after each regexp? Or is  
it a better way to do this?

/Anders V.  
[http://www.vesterberg.se](http://www.vesterberg.se)

---

<div class="post-metadata">

**Author:** ![Anders\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@Anders\_V](https://www.ruby-forum.com/u/Anders_V)\
**Post date:** [August 8, 2006, 11:38am UTC](https://www.ruby-forum.com/t/value-of-1is-retained/101844/2 "2006-08-08T11:38:18Z")

</div>

On 8/8/06, Anders V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # assigning $1 to something
> 
> end
> 
> Shouldn’t the $-variables be reset after each regexp? Or is  
> it a better way to do this?
> 
> /Anders V.  
> [http://www.vesterberg.se](http://www.vesterberg.se)

The problem is in your regex. It will match a word containing hyphens  
or :  
but it will also match an empty string!

> >

a = /"?([\w-:]_)"?/  
=\>  
/"?([\w-:]_)"?/

> >

“This is a String” =~ a  
=\>  
0

> >

$1  
=\>  
“This”

> >

“” =~ a  
=\>  
0

> >

$1  
=\>  
“”

> >

“$12dc” =~ a  
=\>  
0

> >

$1  
=\> “”

Without knowing very much about the requirements of your regex, you need  
to  
replace the \* wildcard ( zero or more ) with + (one or more)

> >

a = /"?([\w-:]+)"?/  
=\>  
/"?([\w-:]+)"?/

> >

“This is a String” =~ a  
=\>  
0

> >

$1  
=\>  
“This”

> >

“” =~ a  
=\>  
nil

> >

$1  
=\>  
nil

> >

“$12dc” =~ a  
=\>  
1

> >

$1  
=\> “12dc”

Nils as you would expect. You could also make the first line it a touch  
more rubyish 😉

key\_colnames.each do |key|  
person\_data[key] =~ /"?([\w-:])"/

# do your thing with $1

end

---

<div class="post-metadata">

**Author:** ![Anders\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@Anders\_V](https://www.ruby-forum.com/u/Anders_V)\
**Post date:** [August 8, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/value-of-1is-retained/101844/3 "2006-08-08T15:32:52Z")

</div>

Anders V. wrote:

> end
> 
> Shouldn’t the $-variables be reset after each regexp? Or is  
> it a better way to do this?

I know this doesn’t answer your question as to why it works that way,  
but you could instead write:

(0…key\_colnames.length-1).each do |i|  
if person\_data[key\_colnames[i]] =~ /"?([\w-:]\*)"?/  
# assigning $1 to something  
end  
end

or even:

(0…key\_colnames.length-1).each do |i|  
myvar = (person\_data[key\_colnames[i]] =~ /"?([\w-:]\*)"?/) ?  
$1 :  
nil  
end

Incidentally, what version of Ruby are you using? I was unable to  
reproduce this behavior on Ruby 1.8.4 on Windows. This sample program:

“boom” =~ /(o)/  
puts “$1 = #{$1.inspect}”

“boom” =~ /(a)/  
puts “$1 = #{$1.inspect}”

“boom” =~ /(o)/  
puts “$1 = #{$1.inspect}”

yields this output:

$1 = “o”  
$1 = nil  
$1 = “o”

---

<div class="post-metadata">

**Author:** ![Anders\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@Anders\_V](https://www.ruby-forum.com/u/Anders_V)\
**Post date:** [August 8, 2006, 3:38pm UTC](https://www.ruby-forum.com/t/value-of-1is-retained/101844/4 "2006-08-08T15:38:53Z")

</div>

Hi –

On Tue, 8 Aug 2006, Anders V. wrote:

> end
> 
> Shouldn’t the $-variables be reset after each regexp? Or is  
> it a better way to do this?

If you’re calling =~ on nil, no match operation takes place.  
Object#=~ returns false, and NilClass doesn’t override it.

David
