How to keep list of reglar expressions into array

I have morethan one regular expressions
I want to keep all in array and iterate them to check my input matched
or not

reg1 = /ruby:\s*$/i
reg2 = /rails:\s*$/i

input1 = “ruby is my favorite language”

i want to iterate regexp’s array and check where input1 matching.But the
porblem is not the porper way getting regexp in loop .It’s returning
string type or else getting by adding ?-mix infront of regexp

can anybody help

On Mon, Feb 6, 2012 at 12:13 AM, Lucky Nl [email protected] wrote:

i want to iterate regexp’s array and check where input1 matching.But the
porblem is not the porper way getting regexp in loop .It’s returning
string type

can anybody help

I have absolutely no idea what this means.

On Mon, Feb 6, 2012 at 2:13 PM, Lucky Nl [email protected] wrote:

porblem is not the porper way getting regexp in loop .It’s returning
string type

try,

input1 = “Matz’s Ruby is my favorite language”
=> “Matz’s Ruby is my favorite language”

regs = [/ruby/i , /rails/i]
=> [/ruby/i, /rails/i]

regs.map do |re| re=~input1 end
=> [7, nil]

regs.group_by do |re| re=~input1 end
=> {7=>[/ruby/i], nil=>[/rails/i]}

best regards
-botp

On Mon, Feb 6, 2012 at 7:13 AM, Lucky Nl [email protected] wrote:

I have morethan one regular expressions
I want to keep all in array and iterate them to check my input matched
or not

reg1 = /ruby:\s*$/i
reg2 = /rails:\s*$/i

In this case it’s easier to use one regexp:

/r(?:uby|ails):\s*$/i

input1 = “ruby is my favorite language”

i want to iterate regexp’s array and check where input1 matching.But the
porblem is not the porper way getting regexp in loop .It’s returning
string type

Please show the code and the error.

You can do

if regexps.any? {|rx| rx =~ s}
puts “any matched”
end

r = regexps.find {|rx| rx =~ s} and
puts “#{r} matched”

puts “#{regexps.select {|rx| rx =~ s}} all matched”

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs