Regex and ruby

Hi,

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

Example:

IN:
fred.smith 1970
sarah 1980
dr cooper

OUT:
fred.smith 1970
sarah 1980

any tips are appreciated, thanks.

On Sat, Dec 7, 2013 at 3:36 PM, smurf smurfing [email protected]
wrote:

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

Are you new to programming and regular expressions in general?

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

On Sun, 8 Dec 2013, smurf smurfing wrote:

fred.smith 1970
sarah 1980
dr cooper

OUT:
fred.smith 1970
sarah 1980

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.

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

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

On Sun, 8 Dec 2013, smurf smurfing wrote:

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

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

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 Dec 7, 2013, at 17:20, Matt L. [email protected] wrote:

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

ri String.match

good points Matt… great thanks.

Using grep:

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

->> puts c
fred.smith 1970
sarah 1980

=)