Regex and ruby

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/.

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

twitter: @hassan

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

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/\.

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

->> puts c
  fred.smith 1970
  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/\.

yes, I am new.. you gotta start somewhere :slight_smile:

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

···

--
Posted via http://www.ruby-forum.com/.

good points Matt.. great thanks.

···

--
Posted via http://www.ruby-forum.com/.

`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

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 :slight_smile:

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