I am trying to write a method to pulls out every string that matches the criteria from a large text file:
- Every element is separated by a comma
- The first 5 elements could be any number from 1-59
- The next 21 elements should be numbers from 1-5
- The next 27 elements could be either true or false (no caps)
- The final 5 elements are integers from 1-5
My code:
#this string should be returned by the regex matching
str="3,15,14,31,40,5,5,4,5,3,4,4,5,2,2,2,1,2,1,1,3,3,3,2,4,3,false,false,false,false,false,true,false,true,false,false,false,false,false,false,false,true,false,false,false,false,false,false,false,false,false,false,false,3,3,3,2,3"
matchResult=/[1-59]{5}[1-5]{21}[true|false]{27}[1-5]{5}/.match(str)
matchResult.each{|x| #this doesnt work....why?
puts x
}
- What is the correct way to print all matches?
matchResult.eachthrows an error. I thought it returned an array of matches. - How can I adjust my regex to expect a comma between every value (not at the ends of the string)?