# Regex and ruby

**URL:** <https://rubytalk.org/t/regex-and-ruby/68854>\
**Category:** ruby-talk\
**Created:** [7 December 2013 23:36 UTC](https://rubytalk.org/t/regex-and-ruby/68854 "2013-12-07T23:36:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [7 December 2013 23:36 UTC](https://rubytalk.org/t/regex-and-ruby/68854/1 "2013-12-07T23:36:54Z")

</div>

Hi,

I'm new to Ruby. I'm trying to scan an array for lines that match the  
following format:

\<string\> \<integer\>

Example:

IN:  
fred.smith 1970  
sarah 1980  
dr cooper

OUT:  
fred.smith 1970  
sarah 1980

any tips are appreciated, thanks.

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

---

<div class="post-metadata">

**Author:** ![Hassan\_Schroeder](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/hassan_schroeder/32/1838_2.png) [@Hassan\_Schroeder](https://rubytalk.org/u/Hassan_Schroeder)\
**Post date:** [7 December 2013 23:52 UTC](https://rubytalk.org/t/regex-and-ruby/68854/2 "2013-12-07T23:52:44Z")

</div>

Are you new to programming and regular expressions in general?

Those examples are pretty basic; what have you tried so far?

> **···**
>
> On Sat, Dec 7, 2013 at 3:36 PM, smurf smurfing \<lists@ruby-forum.com\> wrote:
> 
> > I'm new to Ruby. I'm trying to scan an array for lines that match the  
> > following format:
> > 
> > \<string\> \<integer\>
> 
> --  
> Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
> 
> > **[Hassan Schroeder on about.me](https://about.me/hassanschroeder)**
> >
> > I use about.me to show people what matters most to me.
> 
> twitter: @hassan

---

<div class="post-metadata">

**Author:** ![Matt\_Lawrence1](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@Matt\_Lawrence1](https://rubytalk.org/u/Matt_Lawrence1)\
**Post date:** [8 December 2013 01:11 UTC](https://rubytalk.org/t/regex-and-ruby/68854/3 "2013-12-08T01:11:40Z")

</div>

You could do that with grep if you wanted to.

Put together a regex for a string of characters followed by whitespace followed by digits.

Since it looks like a homework question, I'm just giving you general guidance.

Also, at the last Lone Star Ruby Conference, Nell Shamrell did a talk on regular epressions. I think the video is online. It's a good place to start.

-- Matt  
It's not what I know that counts.  
It's what I can remember in time to use.

> **···**
>
> On Sun, 8 Dec 2013, smurf smurfing wrote:
> 
> > Hi,
> > 
> > I'm new to Ruby. I'm trying to scan an array for lines that match the  
> > following format:
> > 
> > \<string\> \<integer\>
> > 
> > Example:
> > 
> > IN:  
> > fred.smith 1970  
> > sarah 1980  
> > dr cooper
> > 
> > OUT:  
> > fred.smith 1970  
> > sarah 1980

---

<div class="post-metadata">

**Author:** ![Stu1](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Stu1](https://rubytalk.org/u/Stu1)\
**Post date:** [8 December 2013 04:37 UTC](https://rubytalk.org/t/regex-and-ruby/68854/4 "2013-12-08T04:37:30Z")

</div>

Using grep:

> **···**
>
> On Sat, Dec 7, 2013 at 5:36 PM, smurf smurfing \<lists@ruby-forum.com\> wrote:
> 
> > Hi,
> > 
> > I'm new to Ruby. I'm trying to scan an array for lines that match the  
> > following format:
> > 
> > \<string\> \<integer\>
> > 
> > Example:
> > 
> > IN:  
> > fred.smith 1970  
> > sarah 1980  
> > dr cooper
> > 
> > OUT:  
> > fred.smith 1970  
> > sarah 1980
> > 
> > any tips are appreciated, thanks.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Stu1](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Stu1](https://rubytalk.org/u/Stu1)\
**Post date:** [8 December 2013 04:38 UTC](https://rubytalk.org/t/regex-and-ruby/68854/5 "2013-12-08T04:38:52Z")

</div>

c= \<\<-EOF  
&nbsp;&nbsp;fred.smith 1970  
&nbsp;&nbsp;sarah 1980  
&nbsp;&nbsp;dr cooper  
&nbsp;&nbsp;EOF  
&nbsp;&nbsp;c.split( "\n").grep /[0-9]/

-\>\> puts c  
&nbsp;&nbsp;fred.smith 1970  
&nbsp;&nbsp;sarah 1980

=)

> **···**
>
> On Sat, Dec 7, 2013 at 10:37 PM, Stu \<stu@rubyprogrammer.net\> wrote:
> 
> > Using grep:
> > 
> > On Sat, Dec 7, 2013 at 5:36 PM, smurf smurfing \<lists@ruby-forum.com\>wrote:
> > 
> > > Hi,
> > > 
> > > I'm new to Ruby. I'm trying to scan an array for lines that match the  
> > > following format:
> > > 
> > > \<string\> \<integer\>
> > > 
> > > Example:
> > > 
> > > IN:  
> > > fred.smith 1970  
> > > sarah 1980  
> > > dr cooper
> > > 
> > > OUT:  
> > > fred.smith 1970  
> > > sarah 1980
> > > 
> > > any tips are appreciated, thanks.
> > > 
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [8 December 2013 01:13 UTC](https://rubytalk.org/t/regex-and-ruby/68854/6 "2013-12-08T01:13:01Z")

</div>

yes, I am new.. you gotta start somewhere 🙂

match.(/^\w+\s\d+$/)

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

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [8 December 2013 01:32 UTC](https://rubytalk.org/t/regex-and-ruby/68854/7 "2013-12-08T01:32:56Z")

</div>

good points Matt.. great thanks.

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

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [8 December 2013 02:15 UTC](https://rubytalk.org/t/regex-and-ruby/68854/8 "2013-12-08T02:15:26Z")

</div>

`ri String.match`

> **···**
>
> On Dec 7, 2013, at 17:20, Matt Lawrence \<matt@technoronin.com\> wrote:
> 
> > match is a method on regular expressions, so /^\w+\s\d+$/.match(text) would be the right syntax

---

<div class="post-metadata">

**Author:** ![Matt\_Lawrence1](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@Matt\_Lawrence1](https://rubytalk.org/u/Matt_Lawrence1)\
**Post date:** [8 December 2013 01:20 UTC](https://rubytalk.org/t/regex-and-ruby/68854/9 "2013-12-08T01:20:01Z")

</div>

match is a method on regular expressions, so /^\w+\s\d+$/.match(text) would be the right syntax

I don't think "." will be matched by \w, so you may need to use a match for non-whitespace characters.

If I was writing it, I would allow for multiple whitespace characters as dividers and accept trailing whitespace (just because it's easy to have by accident).

-- Matt  
It's not what I know that counts.  
It's what I can remember in time to use.

> **···**
>
> On Sun, 8 Dec 2013, smurf smurfing wrote:
> 
> > yes, I am new.. you gotta start somewhere 🙂
> > 
> > match.(/^\w+\s\d+$/)
