From the array of string I need to get string which starts with age- followed by maximum of 2 digit number and optional '+' sign.
Ex: age-1, age-22, age55, age-1+, age-15+
Following is my array:
arr = ["vintage-colllections","age-5"]
or
arr = ["vintage-colllections","age-51+"]
I will extract age "age-5" or "age-51+" from the array.
I tried following things:
arr.find {|e| e.include?"age-"}
Works well for other scenarios but in above the 1st element of array also includes (vint)age- failing there.
arr.find { |e| /age-\d*\+?/ =~ e}
Works fine but I am trying to avoid regex.
Is there any other better approach ?. Any suggestions are welcome.
if e starts with "age-"? Are there any other elements that start with that?"age-5"orage-51+from strings contained in those two particular arrays. That is, since you know they are there, what is the point of extracting them? Presumably you want to do something like the following: given an array of strings, match each element of the array with a substring that begins "age-", is followed by a greedy match of one or more digits, optionally followed by+". You need to state your question in such a manner."age-"?"page-"?"age-+"(as your regex does)?"age-77#"?"age-4+3"? You cannot expect readers to infer the matching rules by example. If this were a spec for code, there would be blood on the floor. Guess whose?