# Ruby regexp Match

**URL:** <https://www.ruby-forum.com/t/ruby-regexp-match/122041>\
**Category:** Ruby\
**Created:** [December 4, 2007, 3:21am UTC](https://www.ruby-forum.com/t/ruby-regexp-match/122041 "2007-12-04T03:21:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 4, 2007, 3:21am UTC](https://www.ruby-forum.com/t/ruby-regexp-match/122041/1 "2007-12-04T03:21:33Z")

</div>

I am trying to convert some old Perl scripts to Ruby and am stuck at a  
spot where my Perl script is doing a regular expression match on some  
text pulled from a webpage.

In Perl, I was able to grab the text I wanted by putting () around the  
regular expression which represented the data I wanted, then access it  
with the x[0] method.

Is there something like this in Ruby? I was looking through the String  
class and don’t really see a method listed to do this.

I want to be able to do a kind of match like:

```
    m/^Title([a-zA-Z0-9])//g

```

and access what’s inside the () with x[0].

Thanks

jackster

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 4, 2007, 3:39am UTC](https://www.ruby-forum.com/t/ruby-regexp-match/122041/2 "2007-12-04T03:39:36Z")

</div>

On 12/4/07, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to be able to do a kind of match like:  
> m/^Title([a-zA-Z0-9])//g  
> and access what’s inside the () with x[0].

ruby gives more (objects), see  
[http://ruby-doc.org/core/classes/MatchData.html](http://ruby-doc.org/core/classes/MatchData.html)

some simple examples

t=“Title123asdf”  
#=\> “Title123asdf”  
/^Title([a-zA-Z0-9])/.match(t)  
#=\> #MatchData:0x28b7b60  
/^Title([a-zA-Z0-9])/.match(t)[0]  
#=\> “Title1”  
/^Title([a-zA-Z0-9])/.match(t)[1]  
#=\> “1”  
$~  
#=\> #MatchData:0x28afc94  
$~[0]  
#=\> “Title1”  
$~[1]  
#=\> “1”  
x=/^Title([a-zA-Z0-9])/.match(t)  
#=\> #MatchData:0x28a8598  
x[0]  
#=\> “Title1”  
x[1]

kind regards -botp

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 4, 2007, 3:41am UTC](https://www.ruby-forum.com/t/ruby-regexp-match/122041/3 "2007-12-04T03:41:22Z")

</div>

John S. wrote:

> I want to be able to do a kind of match like:
> 
> ```
> m/^Title([a-zA-Z0-9])//g
> 
> ```
> 
> and access what’s inside the () with x[0].
> 
> Thanks
> 
> jackster

Yes.

irb(main):001:0\> result = “Title1”.match(/^Title([a-zA-Z0-9])/)  
=\> #MatchData:0xb7d90654  
irb(main):002:0\> result[1]  
=\> “1”  
irb(main):003:0\> $1  
=\> “1”  
irb(main):004:0\> Regexp.last\_match[1]  
=\> “1”  
irb(main):005:0\> $~[1]  
=\> “1”

[http://ruby-doc.org/core/classes/MatchData.html](http://ruby-doc.org/core/classes/MatchData.html)

-Justin

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 4, 2007, 3:54am UTC](https://www.ruby-forum.com/t/ruby-regexp-match/122041/4 "2007-12-04T03:54:12Z")

</div>

I got it!

thanks to everyone that posted a reply…you all were very helpful!  
jackster

Collins wrote:

> John S. wrote:
> 
> > I want to be able to do a kind of match like:
> > 
> > ```
> > m/^Title([a-zA-Z0-9])//g
> > 
> > ```
> > 
> > and access what’s inside the () with x[0].
> > 
> > Thanks
> > 
> > jackster
> 
> Yes.
> 
> irb(main):001:0\> result = “Title1”.match(/^Title([a-zA-Z0-9])/)  
> =\> #MatchData:0xb7d90654  
> irb(main):002:0\> result[1]  
> =\> “1”  
> irb(main):003:0\> $1  
> =\> “1”  
> irb(main):004:0\> Regexp.last\_match[1]  
> =\> “1”  
> irb(main):005:0\> $~[1]  
> =\> “1”
> 
> [class MatchData - RDoc Documentation](http://ruby-doc.org/core/classes/MatchData.html)
> 
> -Justin

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 4, 2007, 4:31am UTC](https://www.ruby-forum.com/t/ruby-regexp-match/122041/5 "2007-12-04T04:31:03Z")

</div>

On Dec 3, 8:54 pm, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > I want to be able to do a kind of match like:
> 
> > [class MatchData - RDoc Documentation](http://ruby-doc.org/core/classes/MatchData.html)
> 
> > -Justin
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

You can also do like you would in perl…

“TitleA” =~ /^Title([a-zA-Z0-9])/  
puts $1

# =\> A

Regards,  
Jordan
