# Regex question

**URL:** <https://rubytalk.org/t/regex-question/19940>\
**Category:** ruby-talk\
**Created:** [4 August 2005 00:13 UTC](https://rubytalk.org/t/regex-question/19940 "2005-08-04T00:13:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [4 August 2005 00:13 UTC](https://rubytalk.org/t/regex-question/19940/1 "2005-08-04T00:13:20Z")

</div>

I have a file that contains the following contents:

sw\_corner = 1000,-1000  
ne\_corner = -1000,1000

I want to read that file and figure out what the sw\_corner and  
ne\_corner values are. Here's my following attempt, but it looks ugly.  
How can I improve it?

sw\_corner = map\_data.scan(/sw\_corner = ([-\d]+),\s\*([-\d]+)/)[0]  
ne\_corner = map\_data.scan(/ne\_corner = ([-\d]+),\s\*([-\d]+)/)[0]

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [4 August 2005 00:25 UTC](https://rubytalk.org/t/regex-question/19940/2 "2005-08-04T00:25:15Z")

</div>

(untested)

&nbsp;&nbsp;&nbsp;h = {}  
&nbsp;&nbsp;&nbsp;map\_data.each do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key, value = line.split %r/=/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h[k.strip] = value.split(%r/,/).map{|n| Integer n}  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;p h['sw\_corner']  
&nbsp;&nbsp;&nbsp;p h['nw\_corner']

hth.

-a

> **···**
>
> On Thu, 4 Aug 2005, Joe Van Dyk wrote:
> 
> > I have a file that contains the following contents:
> > 
> > sw\_corner = 1000,-1000  
> > ne\_corner = -1000,1000
> > 
> > I want to read that file and figure out what the sw\_corner and  
> > ne\_corner values are. Here's my following attempt, but it looks ugly.  
> > How can I improve it?
> > 
> > sw\_corner = map\_data.scan(/sw\_corner = ([-\d]+),\s\*([-\d]+)/)[0]  
> > ne\_corner = map\_data.scan(/ne\_corner = ([-\d]+),\s\*([-\d]+)/)[0]
> > 
> > Thanks,  
> > Joe
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > My religion is very simple. My religion is kindness.  
> > --Tenzin Gyatso
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [4 August 2005 00:46 UTC](https://rubytalk.org/t/regex-question/19940/3 "2005-08-04T00:46:49Z")

</div>

Hi --

> I have a file that contains the following contents:
> 
> sw\_corner = 1000,-1000  
> ne\_corner = -1000,1000
> 
> I want to read that file and figure out what the sw\_corner and  
> ne\_corner values are. Here's my following attempt, but it looks ugly.  
> How can I improve it?
> 
> sw\_corner = map\_data.scan(/sw\_corner = ([-\d]+),\s\*([-\d]+)/)[0]  
> ne\_corner = map\_data.scan(/ne\_corner = ([-\d]+),\s\*([-\d]+)/)[0]

Here's one possibility, if you want them as integers:

require 'scanf'

map\_data = \<\<EOM  
sw\_corner = 1000,-1000  
ne\_corner = -1000,1000  
EOM

format = "%\*s = %d,%d"

sw = map\_data.scanf(format)  
ne = map\_data.scanf(format)

puts "sw is #{sw.inspect},ne is #{ne.inspect}"

David

> **···**
>
> On Thu, 4 Aug 2005, Joe Van Dyk wrote:
> 
> --
> 
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [4 August 2005 02:51 UTC](https://rubytalk.org/t/regex-question/19940/4 "2005-08-04T02:51:06Z")

</div>

Joe Van Dyk wrote:

> I have a file that contains the following contents:
> 
> sw\_corner = 1000,-1000  
> ne\_corner = -1000,1000
> 
> I want to read that file and figure out what the sw\_corner and  
> ne\_corner values are. Here's my following attempt, but it looks ugly.  
> How can I improve it?
> 
> sw\_corner = map\_data.scan(/sw\_corner = ([-\d]+),\s\*([-\d]+)/)[0]  
> ne\_corner = map\_data.scan(/ne\_corner = ([-\d]+),\s\*([-\d]+)/)[0]

map\_data = DATA.read

x = map\_data.scan( /(sw|ne)\_corner = (-?\d+),\s\*(-?\d+)/ )  
sw\_corner = x.assoc("sw")[1,2]  
ne\_corner = x.assoc("ne")[1,2]

\_\_END\_\_  
sw\_corner = 1000,-1000  
ne\_corner = -1000,1000

---

<div class="post-metadata">

**Author:** ![Caleb\_Clausen1](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@Caleb\_Clausen1](https://rubytalk.org/u/Caleb_Clausen1)\
**Post date:** [4 August 2005 02:05 UTC](https://rubytalk.org/t/regex-question/19940/5 "2005-08-04T02:05:34Z")

</div>

Then there's always:

eval "filename" #😉

which will have exactly the right semantics in your case.

> **···**
>
> On 8/3/05, David A. Black \<dblack@wobblini.net\> wrote:
> 
> > Hi --
> > 
> > On Thu, 4 Aug 2005, Joe Van Dyk wrote:
> > 
> > \> I have a file that contains the following contents:  
> > \>  
> > \> sw\_corner = 1000,-1000  
> > \> ne\_corner = -1000,1000  
> > \>  
> > \> I want to read that file and figure out what the sw\_corner and  
> > \> ne\_corner values are. Here's my following attempt, but it looks ugly.  
> > \> How can I improve it?  
> > \>  
> > \> sw\_corner = map\_data.scan(/sw\_corner = ([-\d]+),\s\*([-\d]+)/)[0]  
> > \> ne\_corner = map\_data.scan(/ne\_corner = ([-\d]+),\s\*([-\d]+)/)[0]
> > 
> > Here's one possibility, if you want them as integers:
> > 
> > require 'scanf'
> > 
> > map\_data = \<\<EOM  
> > sw\_corner = 1000,-1000  
> > ne\_corner = -1000,1000  
> > EOM
> > 
> > format = "%\*s = %d,%d"
> > 
> > sw = map\_data.scanf(format)  
> > ne = map\_data.scanf(format)
> > 
> > puts "sw is #{sw.inspect},ne is #{ne.inspect}"
> > 
> > David
> > 
> > --
> > 
> > David A. Black  
> > dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [4 August 2005 02:23 UTC](https://rubytalk.org/t/regex-question/19940/6 "2005-08-04T02:23:43Z")

</div>

> Then there's always:
> 
> eval "filename" #😉
> 
> which will have exactly the right semantics in your case.

Ah... I was trying to do instance\_eval. Why didn't that work?

> **···**
>
> On 8/3/05, Caleb Clausen \<vikkous@gmail.com\> wrote:
> 
> > On 8/3/05, David A. Black \<dblack@wobblini.net\> wrote:  
> > \> Hi --  
> > \>  
> > \> On Thu, 4 Aug 2005, Joe Van Dyk wrote:  
> > \>  
> > \> \> I have a file that contains the following contents:  
> > \> \>  
> > \> \> sw\_corner = 1000,-1000  
> > \> \> ne\_corner = -1000,1000  
> > \> \>  
> > \> \> I want to read that file and figure out what the sw\_corner and  
> > \> \> ne\_corner values are. Here's my following attempt, but it looks ugly.  
> > \> \> How can I improve it?  
> > \> \>  
> > \> \> sw\_corner = map\_data.scan(/sw\_corner = ([-\d]+),\s\*([-\d]+)/)[0]  
> > \> \> ne\_corner = map\_data.scan(/ne\_corner = ([-\d]+),\s\*([-\d]+)/)[0]  
> > \>  
> > \> Here's one possibility, if you want them as integers:  
> > \>  
> > \> require 'scanf'  
> > \>  
> > \> map\_data = \<\<EOM  
> > \> sw\_corner = 1000,-1000  
> > \> ne\_corner = -1000,1000  
> > \> EOM  
> > \>  
> > \> format = "%\*s = %d,%d"  
> > \>  
> > \> sw = map\_data.scanf(format)  
> > \> ne = map\_data.scanf(format)  
> > \>  
> > \> puts "sw is #{sw.inspect},ne is #{ne.inspect}"  
> > \>  
> > \>  
> > \> David  
> > \>  
> > \> --  
> > \>  
> > \>  
> > \> David A. Black  
> > \> dblack@wobblini.net  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [4 August 2005 03:02 UTC](https://rubytalk.org/t/regex-question/19940/7 "2005-08-04T03:02:53Z")

</div>

Hi --

> Then there's always:
> 
> eval "filename" #😉
> 
> which will have exactly the right semantics in your case.

But variable scope problems:

$ cat corners.txt sw\_corner = 1000,-1000  
ne\_corner = -1000,1000  
$ ruby -e 'eval(File.read("corners.txt")); p sw\_corner'  
-e:1: undefined local variable or method `sw\_corner' for main:Object  
(NameError)

(Yes, you can pre-initialize them. It still seems fragile and has the  
usual eval issues.)

David

> **···**
>
> On Thu, 4 Aug 2005, Caleb Clausen wrote:
> 
> > On 8/3/05, David A. Black \<dblack@wobblini.net\> wrote:
> > 
> > > Hi --
> > > 
> > > On Thu, 4 Aug 2005, Joe Van Dyk wrote:
> > > 
> > > > I have a file that contains the following contents:
> > > > 
> > > > sw\_corner = 1000,-1000  
> > > > ne\_corner = -1000,1000
> > > > 
> > > > I want to read that file and figure out what the sw\_corner and  
> > > > ne\_corner values are. Here's my following attempt, but it looks ugly.  
> > > > How can I improve it?
> > > > 
> > > > sw\_corner = map\_data.scan(/sw\_corner = ([-\d]+),\s\*([-\d]+)/)[0]  
> > > > ne\_corner = map\_data.scan(/ne\_corner = ([-\d]+),\s\*([-\d]+)/)[0]
> > > 
> > > Here's one possibility, if you want them as integers:
> > > 
> > > require 'scanf'
> > > 
> > > map\_data = \<\<EOM  
> > > sw\_corner = 1000,-1000  
> > > ne\_corner = -1000,1000  
> > > EOM
> > > 
> > > format = "%\*s = %d,%d"
> > > 
> > > sw = map\_data.scanf(format)  
> > > ne = map\_data.scanf(format)
> > > 
> > > puts "sw is #{sw.inspect},ne is #{ne.inspect}"
> > > 
> > > David
> > > 
> > > --
> > > 
> > > David A. Black  
> > > dblack@wobblini.net
> 
> --  
> David A. Black  
> dblack@wobblini.net
