Regexp question (newbie)

After playing a little bit with Ruby 3 years ago, I am trying to learn
it now.
Problem:

a = [“aaa”,“abb”,“bbb”,“acc”,“bcc”,“ccc”]

I want a regular expression which would extract something like [“acc”,
“ccc”] from a

I want something like

irb(main):023:0> for b in a
irb(main):024:1> b.scan("#{(a||c)}cc") && print b
irb(main):025:1> end

but I am not sure how to approach it.

Regars.
Johann

···


Johann Spies Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

 "But my God shall supply all your need according to his
  riches in glory by Christ Jesus."     Philippians 4:19

Hello –

After playing a little bit with Ruby 3 years ago, I am trying to learn
it now.
Problem:

a = [“aaa”,“abb”,“bbb”,“acc”,“bcc”,“ccc”]

I want a regular expression which would extract something like [“acc”,
“ccc”] from a

I want something like

irb(main):023:0> for b in a
irb(main):024:1> b.scan(“#{(a||c)}cc”) && print b
irb(main):025:1> end

but I am not sure how to approach it.

I would use #select, which filters based on a condition contained
in a code block:

a = [“aaa”,“abb”,“bbb”,“acc”,“bcc”,“ccc”]
puts a.select {|e| /(a|c)cc/.match(e)}

=>
acc
ccc

David

···

On Sat, 21 Sep 2002, Johann Spies wrote:


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

Thanks David!

Johann

···

On Sat, Sep 21, 2002 at 12:55:28AM +0900, dblack@candle.superlink.net wrote:

I would use #select, which filters based on a condition contained
in a code block:

a = [“aaa”,“abb”,“bbb”,“acc”,“bcc”,“ccc”]
puts a.select {|e| /(a|c)cc/.match(e)}

=>
acc
ccc


Johann Spies Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

 "But my God shall supply all your need according to his
  riches in glory by Christ Jesus."     Philippians 4:19

I think I’d use Enumerable#grep in this case:

irb(main):001:0> a = [“aaa”,“abb”,“bbb”,“acc”,“bcc”,“ccc”]
[“aaa”, “abb”, “bbb”, “acc”, “bcc”, “ccc”]
irb(main):002:0> a.grep /(a|c)cc/
[“acc”, “ccc”]

~ Bruce

···

On Friday 20 September 2002 11:55 am, dblack@candle.superlink.net wrote:

I would use #select, which filters based on a condition contained
in a code block:

a = [“aaa”,“abb”,“bbb”,“acc”,“bcc”,“ccc”]
puts a.select {|e| /(a|c)cc/.match(e)}

=>
acc
ccc

David


_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/
Bruce Williams http://www.codedbliss.com
iusris/#ruby-lang bruce@codedbliss.com
_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/

Hi –

···

On Sat, 21 Sep 2002, Bruce Williams wrote:

On Friday 20 September 2002 11:55 am, dblack@candle.superlink.net wrote:

I would use #select, which filters based on a condition contained
in a code block:

a = [“aaa”,“abb”,“bbb”,“acc”,“bcc”,“ccc”]
puts a.select {|e| /(a|c)cc/.match(e)}

=>
acc
ccc

David

I think I’d use Enumerable#grep in this case:

irb(main):001:0> a = [“aaa”,“abb”,“bbb”,“acc”,“bcc”,“ccc”]
[“aaa”, “abb”, “bbb”, “acc”, “bcc”, “ccc”]
irb(main):002:0> a.grep /(a|c)cc/
[“acc”, “ccc”]

I always forget that one :slight_smile:

David


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com