# Reg Ex Help Required

**URL:** <https://rubytalk.org/t/reg-ex-help-required/63297>\
**Category:** ruby-talk\
**Created:** [10 August 2011 18:20 UTC](https://rubytalk.org/t/reg-ex-help-required/63297 "2011-08-10T18:20:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![saurabh\_a1](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@saurabh\_a1](https://rubytalk.org/u/saurabh_a1)\
**Post date:** [10 August 2011 18:20 UTC](https://rubytalk.org/t/reg-ex-help-required/63297/1 "2011-08-10T18:20:31Z")

</div>

Hi,

Please help me with regular expression.

str = "abc = 100.300"

i want to write a regular xpression for getting the value after =, i.e.  
100.300, i mean reg ex should return value after =

Please help.

Thanks  
Saurabh

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ian](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@Ian](https://rubytalk.org/u/Ian)\
**Post date:** [10 August 2011 18:27 UTC](https://rubytalk.org/t/reg-ex-help-required/63297/2 "2011-08-10T18:27:21Z")

</div>

ruby-1.8.7-p334 :006 \> str = "abc = 100.300"  
=\> "abc = 100.300"  
ruby-1.8.7-p334 :007 \> /= (.\*)/.match(str)[0]  
=\> "= 100.300"

> **···**
>
> On Wed, Aug 10, 2011 at 2:20 PM, saurabh a. \<saurabh\_anand2002@yahoo.com\>wrote:
> 
> > Hi,
> > 
> > Please help me with regular expression.
> > 
> > str = "abc = 100.300"
> > 
> > i want to write a regular xpression for getting the value after =, i.e.  
> > 100.300, i mean reg ex should return value after =
> > 
> > Please help.
> > 
> > Thanks  
> > Saurabh
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Chris\_White](https://avatars.discourse-cdn.com/v4/letter/c/e56c9b/32.png) [@Chris\_White](https://rubytalk.org/u/Chris_White)\
**Post date:** [10 August 2011 18:30 UTC](https://rubytalk.org/t/reg-ex-help-required/63297/3 "2011-08-10T18:30:54Z")

</div>

Might I suggest an alternative to regex?

irb(main):003:0\> str.split(/ = /)  
=\> ["abc", "100.300"]  
irb(main):004:0\> str.split(/ = /)[1]  
=\> "100.300"  
irb(main):005:0\>

Regards,  
Chris White  
[http://www.twitter.com/cwgem](http://www.twitter.com/cwgem)

> **···**
>
> On Aug 10, 2011, at 11:20 AM, saurabh a. wrote:
> 
> > Hi,
> > 
> > Please help me with regular expression.
> > 
> > str = "abc = 100.300"
> > 
> > i want to write a regular xpression for getting the value after =, i.e.  
> > 100.300, i mean reg ex should return value after =

---

<div class="post-metadata">

**Author:** ![Ian](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@Ian](https://rubytalk.org/u/Ian)\
**Post date:** [10 August 2011 18:28 UTC](https://rubytalk.org/t/reg-ex-help-required/63297/4 "2011-08-10T18:28:16Z")

</div>

sorry, should be

ruby-1.8.7-p334 :006 \> str = "abc = 100.300"  
=\> "abc = 100.300"  
ruby-1.8.7-p334 :008 \> /= (.\*)/.match(str)[1]  
=\> "100.300"

> **···**
>
> On Wed, Aug 10, 2011 at 2:26 PM, Ian M. Asaff \<ian.asaff@gmail.com\> wrote:
> 
> > ruby-1.8.7-p334 :006 \> str = "abc = 100.300"  
> > =\> "abc = 100.300"  
> > ruby-1.8.7-p334 :007 \> /= (.\*)/.match(str)[0]  
> > =\> "= 100.300"
> > 
> > On Wed, Aug 10, 2011 at 2:20 PM, saurabh a. \<saurabh\_anand2002@yahoo.com\>wrote:
> > 
> > > Hi,
> > > 
> > > Please help me with regular expression.
> > > 
> > > str = "abc = 100.300"
> > > 
> > > i want to write a regular xpression for getting the value after =, i.e.  
> > > 100.300, i mean reg ex should return value after =
> > > 
> > > Please help.
> > > 
> > > Thanks  
> > > Saurabh
> > > 
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Dan\_Sr](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@Dan\_Sr](https://rubytalk.org/u/Dan_Sr)\
**Post date:** [10 August 2011 18:46 UTC](https://rubytalk.org/t/reg-ex-help-required/63297/5 "2011-08-10T18:46:21Z")

</div>

alternatively you could make the two inputs variables and the output you are  
trying to recieve a variable as well that way you would never have to worry  
about regexing anything. You would simply reference the variable . . . You  
can of course create a class as well . . .

puts "First Number"  
r = gets.chomp  
puts "Second Number"  
p = gets.chomp  
x = p \* r  
puts x

class Someclass  
def initialize (First Number, Second Number)  
@First Number = First Number  
@Second Number = Second Number  
end  
end

Calc = Someclass.new(0,1)  
x = calc.first \* calc.last

Of course if you're working with floats or strings back and forth just add  
the to\_f or to\_s. I haven't worked on ruby in a while but maybe this will  
help you spawn some ideas.

> **···**
>
> On Wed, Aug 10, 2011 at 1:30 PM, Chris White \<cwprogram@live.com\> wrote:
> 
> > On Aug 10, 2011, at 11:20 AM, saurabh a. wrote:
> > 
> > \> Hi,  
> > \>  
> > \> Please help me with regular expression.  
> > \>  
> > \> str = "abc = 100.300"  
> > \>  
> > \> i want to write a regular xpression for getting the value after =, i.e.  
> > \> 100.300, i mean reg ex should return value after =
> > 
> > Might I suggest an alternative to regex?
> > 
> > irb(main):003:0\> str.split(/ = /)  
> > =\> ["abc", "100.300"]  
> > irb(main):004:0\> str.split(/ = /)[1]  
> > =\> "100.300"  
> > irb(main):005:0\>
> > 
> > Regards,  
> > Chris White  
> > [http://www.twitter.com/cwgem](http://www.twitter.com/cwgem)

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [10 August 2011 22:34 UTC](https://rubytalk.org/t/reg-ex-help-required/63297/6 "2011-08-10T22:34:54Z")

</div>

I'd use split as well, but I'd probably do it like this:

str = "abc = 100.300"  
variable, value = str.split('=', 2).map(&:strip)  
variable # =\> "abc"  
value # =\> "100.300"

> **···**
>
> On Wed, Aug 10, 2011 at 1:30 PM, Chris White \<cwprogram@live.com\> wrote:
> 
> > On Aug 10, 2011, at 11:20 AM, saurabh a. wrote:
> > 
> > \> Hi,  
> > \>  
> > \> Please help me with regular expression.  
> > \>  
> > \> str = "abc = 100.300"  
> > \>  
> > \> i want to write a regular xpression for getting the value after =, i.e.  
> > \> 100.300, i mean reg ex should return value after =
> > 
> > Might I suggest an alternative to regex?
> > 
> > irb(main):003:0\> str.split(/ = /)  
> > =\> ["abc", "100.300"]  
> > irb(main):004:0\> str.split(/ = /)[1]  
> > =\> "100.300"  
> > irb(main):005:0\>
> > 
> > Regards,  
> > Chris White  
> > [http://www.twitter.com/cwgem](http://www.twitter.com/cwgem)

---

<div class="post-metadata">

**Author:** ![11142](https://avatars.discourse-cdn.com/v4/letter/1/e0b2c6/32.png) [@11142](https://rubytalk.org/u/11142)\
**Post date:** [10 August 2011 19:27 UTC](https://rubytalk.org/t/reg-ex-help-required/63297/7 "2011-08-10T19:27:09Z")

</div>

Um, no, why would you need a class to store two numbers? I'd go with  
Chris's split approach. Unless what you really want is to match the  
number, then you do need regex and an example one would be /\d+\.\d+/  
.

> **···**
>
> 2011/8/10, Daniel Navarro \<djonavarro@gmail.com\>:
> 
> > alternatively you could make the two inputs variables and the output you are  
> > trying to recieve a variable as well that way you would never have to worry  
> > about regexing anything. You would simply reference the variable . . . You  
> > can of course create a class as well . . .
> > 
> > puts "First Number"  
> > r = gets.chomp  
> > puts "Second Number"  
> > p = gets.chomp  
> > x = p \* r  
> > puts x
> > 
> > class Someclass  
> > def initialize (First Number, Second Number)  
> > @First Number = First Number  
> > @Second Number = Second Number  
> > end  
> > end
> > 
> > Calc = Someclass.new(0,1)  
> > x = calc.first \* calc.last
> > 
> > Of course if you're working with floats or strings back and forth just add  
> > the to\_f or to\_s. I haven't worked on ruby in a while but maybe this will  
> > help you spawn some ideas.
> > 
> > On Wed, Aug 10, 2011 at 1:30 PM, Chris White \<cwprogram@live.com\> wrote:
> > 
> > > On Aug 10, 2011, at 11:20 AM, saurabh a. wrote:
> > > 
> > > \> Hi,  
> > > \>  
> > > \> Please help me with regular expression.  
> > > \>  
> > > \> str = "abc = 100.300"  
> > > \>  
> > > \> i want to write a regular xpression for getting the value after =, i.e.  
> > > \> 100.300, i mean reg ex should return value after =
> > > 
> > > Might I suggest an alternative to regex?
> > > 
> > > irb(main):003:0\> str.split(/ = /)  
> > > =\> ["abc", "100.300"]  
> > > irb(main):004:0\> str.split(/ = /)[1]  
> > > =\> "100.300"  
> > > irb(main):005:0\>
> > > 
> > > Regards,  
> > > Chris White  
> > > [http://www.twitter.com/cwgem](http://www.twitter.com/cwgem)
> 
> --  
> -- Matma Rex

---

<div class="post-metadata">

**Author:** ![Chad\_Perrin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/chad_perrin/32/2234_2.png) [@Chad\_Perrin](https://rubytalk.org/u/Chad_Perrin)\
**Post date:** [11 August 2011 00:44 UTC](https://rubytalk.org/t/reg-ex-help-required/63297/8 "2011-08-11T00:44:54Z")

</div>

I'd probably use a regex for the split sequence rather than a literal to  
account for variations in whitespace, rather than a map:

&nbsp;&nbsp;&nbsp;&nbsp;variable, value = str.split(/\s\*=\s\*/, 2)

. . . but either way works.

> **···**
>
> On Thu, Aug 11, 2011 at 07:34:54AM +0900, Josh Cheek wrote:
> 
> > I'd use split as well, but I'd probably do it like this:
> > 
> > str = "abc = 100.300"  
> > variable, value = str.split('=', 2).map(&:strip)  
> > variable # =\> "abc"  
> > value # =\> "100.300"
> 
> --  
> Chad Perrin [original content licensed OWL: [http://owl.apotheon.org](http://owl.apotheon.org) ]

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [11 August 2011 01:15 UTC](https://rubytalk.org/t/reg-ex-help-required/63297/9 "2011-08-11T01:15:57Z")

</div>

It seems to me that there could be whitespace on either end, ie

"  
def whatever  
&nbsp;&nbsp;var = 123  
end  
"

This shows a very common example of leading whitespace, so I thought using  
strip would be better.

> **···**
>
> On Wed, Aug 10, 2011 at 7:44 PM, Chad Perrin \<code@apotheon.net\> wrote:
> 
> > On Thu, Aug 11, 2011 at 07:34:54AM +0900, Josh Cheek wrote:  
> > \>  
> > \> I'd use split as well, but I'd probably do it like this:  
> > \>  
> > \> str = "abc = 100.300"  
> > \> variable, value = str.split('=', 2).map(&:strip)  
> > \> variable # =\> "abc"  
> > \> value # =\> "100.300"
> > 
> > I'd probably use a regex for the split sequence rather than a literal to  
> > account for variations in whitespace, rather than a map:
> > 
> > &nbsp;&nbsp;&nbsp;variable, value = str.split(/\s\*=\s\*/, 2)
> > 
> > . . . but either way works.
> > 
> > --  
> > Chad Perrin [original content licensed OWL: [http://owl.apotheon.org](http://owl.apotheon.org) ]

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [11 August 2011 01:17 UTC](https://rubytalk.org/t/reg-ex-help-required/63297/10 "2011-08-11T01:17:46Z")

</div>

Although, I suppose you could make the argument that an actual parser would  
be best.

> **···**
>
> On Wed, Aug 10, 2011 at 8:15 PM, Josh Cheek \<josh.cheek@gmail.com\> wrote:
> 
> > On Wed, Aug 10, 2011 at 7:44 PM, Chad Perrin \<code@apotheon.net\> wrote:
> > 
> > > On Thu, Aug 11, 2011 at 07:34:54AM +0900, Josh Cheek wrote:  
> > > \>  
> > > \> I'd use split as well, but I'd probably do it like this:  
> > > \>  
> > > \> str = "abc = 100.300"  
> > > \> variable, value = str.split('=', 2).map(&:strip)  
> > > \> variable # =\> "abc"  
> > > \> value # =\> "100.300"
> > > 
> > > I'd probably use a regex for the split sequence rather than a literal to  
> > > account for variations in whitespace, rather than a map:
> > > 
> > > &nbsp;&nbsp;&nbsp;variable, value = str.split(/\s\*=\s\*/, 2)
> > > 
> > > . . . but either way works.
> > > 
> > > --  
> > > Chad Perrin [original content licensed OWL: [http://owl.apotheon.org](http://owl.apotheon.org) ]
> > 
> > It seems to me that there could be whitespace on either end, ie
> > 
> > "  
> > def whatever  
> > &nbsp;&nbsp;var = 123  
> > end  
> > "
> > 
> > This shows a very common example of leading whitespace, so I thought using  
> > strip would be better.

---

<div class="post-metadata">

**Author:** ![Chad\_Perrin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/chad_perrin/32/2234_2.png) [@Chad\_Perrin](https://rubytalk.org/u/Chad_Perrin)\
**Post date:** [11 August 2011 01:29 UTC](https://rubytalk.org/t/reg-ex-help-required/63297/11 "2011-08-11T01:29:03Z")

</div>

Did I miss the part where someone said that the "var" part is actually  
something the code should preserve separately?

> **···**
>
> On Thu, Aug 11, 2011 at 10:15:57AM +0900, Josh Cheek wrote:
> 
> > It seems to me that there could be whitespace on either end, ie
> > 
> > "  
> > def whatever  
> > &nbsp;&nbsp;var = 123  
> > end  
> > "
> > 
> > This shows a very common example of leading whitespace, so I thought  
> > using strip would be better.
> 
> --  
> Chad Perrin [original content licensed OWL: [http://owl.apotheon.org](http://owl.apotheon.org) ]
