0

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.

10
  • 1
    Is it always the second element in the array? Commented Feb 16, 2016 at 4:36
  • 1
    What about if e starts with "age-"? Are there any other elements that start with that? Commented Feb 16, 2016 at 4:39
  • 2
    I'm curious why you don't want to use a regex here, as it is clearly the way to go. Is it just an exercise? Commented Feb 16, 2016 at 4:42
  • 2
    Your problem is not that you want to extract "age-5" or age-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. Commented Feb 16, 2016 at 4:52
  • 1
    I was not just making a suggestion for the future; you need to clarify the question! Do you want to match "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? Commented Feb 16, 2016 at 8:41

2 Answers 2

5

Use start_with?:

arr.find { |e| e.start_with?("age-") }
Sign up to request clarification or add additional context in comments.

2 Comments

That was good and almost 98% correct solution. I will hope some other elemnts not starts with "age-" (i.e :some stupid string like : age-rope).
Though regex is the best solution in this case, you may try something strange like arr.find { |e| e.gsub("age-", '').to_i > 0 }
2

I must grit my teeth to not use a regex, but here goes. I assume the question is as described in a comment I left on the question.

def find_str(arr)
  arr.map { |str| str_match(str) }.compact
end

def str_match(str)
  return nil unless str[0,4] == "age-"
  last = str[-1] == '+' ? -2 : -1
  Integer(str[4..last]) rescue return nil
  str
end

find_str ["cat", "age-5"]     #=> ["age-5"] 
find_str ["cat", "age-51+"]   #=> ["age-51+"] 
find_str ["cat", "age-5t1+"]  #=> [] 
find_str ["cat", "xage-51+"]  #=> [] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.