I want to match the options between two arrays with exact string.
options = ["arish1", "arish2", "ARISH3", "arish 2", "arish"]
choices = ["Arish"]
final_choice = options.grep(Regexp.new(choices.join('|'), Regexp::IGNORECASE))
p final_choice
Output:
["arish1", "arish2", "ARISH3", "arish 2", "arish"]
but it should be only match "arish"
You need to use
See the Ruby online demo.
Note:
Regexp.unionmethod joins the alternatives inchoicesusing|"or" regex operator and escapes the items as necessary automatically\Aanchor matches the start of stirng and\zmatches the end of stirng.(?:...), is used to make sure the anchors are applied to each alternative inchoicesseparately..sourceis used to obtain just the pattern part from the regex.