Dave
Dave

Reputation: 19130

How do I find consecutive strings in my array that match a regex?

I'm using Ruby 2.4. I want to find consecutive tokens in my array of strings that match a regular expression. So if my regex is

/\p{L}/

and my array is

 ["2917", "m", "neatty", "fff", "46", "u", "28", "56"]

I would want the result to be

["m", "neatty", "fff"]

However, my attempt to do this has failed (notice the "neatty" token is repeated) ...

2.4.0 :020 > arr = ["2917", "m", "neatty", "fff", "46", "u", "28", "56"]
 => ["2917", "m", "neatty", "fff", "46", "u", "28", "56"]
2.4.0 :021 > arr.each_cons(2).select{|pair| pair.all?{|elem| elem =~ /\p{L}/ }}.flatten
 => ["m", "neatty", "neatty", "fff"]

How do I find consecutive tokens in an array that match a pattern that also don't repeat?

Upvotes: 3

Views: 277

Answers (3)

Cary Swoveland
Cary Swoveland

Reputation: 110675

arr = ["2917", "m", "neatty", "fff", "46", "u", "28", "56", "hi", "%ya!"]
r = /\p{L}/

arr.each_with_object([[]]) { |s,a| s.match?(r) ? (a.last << s) : a << [] }.
    reject { |a| a.size < 2 }
  #=> [["m", "neatty", "fff"], ["hi", "%ya!"]]

Upvotes: 0

dawg
dawg

Reputation: 103814

You can also use slice_when to find borders of the sub array that bound the condition:

> arr.slice_when {|x,y| !x[reg] || !y[reg] }.select {|e| e.length>1}
=> [["m", "neatty", "fff"]]

Upvotes: 2

Sagar Pandya
Sagar Pandya

Reputation: 9497

If r is your regex then use chunk_while

arr.chunk_while { |a,b| a[r] && b[r] }.select { |arr| arr.size > 1 }     
 #=> [["m", "neatty", "fff"]]

Upvotes: 3

Related Questions