How do I find consecutive strings in my array that match a regular expression?
I am using Ruby 2.4. I want to find sequential tokens in my array of strings that match a regex. So if my regex is
/\p{L}/
and my array
["2917", "m", "neatty", "fff", "46", "u", "28", "56"]
I want the result to be
["m", "neatty", "fff"]
However, my attempt to do this has failed (note that the "neat" 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 sequential tokens in an array that match a pattern that doesn't repeat itself either?
+3
source to share
3 answers