From: Brian Candler Date: 2012-06-13T14:09:00+09:00 Subject: Re: Problem with reg exp > str1 = "ABC12A2012" > if str1 =~ /^([a-zA-Z]+)(.*?)((?:KEYWORD)?)$/ > p $1, $2, $3 # => "ABC", "12A2012", "" > end > > str2 = "ABC13B2012KEYWORD" > if str2 =~ /^([a-zA-Z]+)(.*?)((?:KEYWORD)?)$/ > p $1, $2, $3 # => "ABC", "13B2013", "KEYWORD" > end > > If you don't like using the if-statement structure, you can also use > String#match , which returns a MatchData object. You can access the > groups using array syntax: You can also look at String#scan, if a single string contains multiple instances of what you're trying to match. -- Posted via http://www.ruby-forum.com/.