From: William James Date: 2007-12-07T01:05:19+09:00 Subject: Re: regular expression. newbie problem. On Dec 6, 9:42 am, Johnathan Smith wrote: > Hi, > > i know this is a pretty basic problem but im a newbie so any help would > be greatly appreciated > > im trying to write a name class one which returns a first name a > surname, first and second initials > > however, i realise i regular expression is needed to go through the name > to produce the correct output. however im unsure of whaty regular > expression to use and whether my class is looking correct in general > > anyhelp would be greatly appreciated > > thanks > > name class: > > class Name > > def initialize(name) > @fname = name.slice!(rexesp) > @sname = name.slice!(regexp) > @mname = name.slice!(regexp) > > def firstname > return @fname > end > > def surname > return @sname > end > > def firstinitial > return @finitial > end > > def firstinitial > return @sinitial > end > -- > Posted viahttp://www.ruby-forum.com/. p "Chidiock Tichborne".strip.match( /^(\S+)( +\S.*)? +(\S+)$/ ).to_a p "Edgar A. Poe".strip.match( /^(\S+)( +\S.*)? +(\S+)$/ ).to_a p "J. R. R. Tolkien".strip.match( /^(\S+)( +\S.*)? +(\S+)$/ ).to_a _,first,middle,sur = "Ambrose Bierce". strip.match( /^(\S+)( +\S.*)? +(\S+)$/ ).to_a puts "#{ first }:#{ middle }:#{ sur }" ---- output ---- ["Chidiock Tichborne", "Chidiock", nil, "Tichborne"] ["Edgar A. Poe", "Edgar", " A.", "Poe"] ["J. R. R. Tolkien", "J.", " R. R.", "Tolkien"] Ambrose::Bierce