How do I efficiently reject Strings in an array if they (regex) match Strings in a second array in Ruby? -
I have two strings of strings, for example sentences and words if a word is found in a sentence eg Sentence = ~ / # {word} /
I want to reject the sentence from the sentence array. It's easy to do this with double loop, but I'm thinking that there is a more efficient way to do this, perhaps with logical operators?
You can add all the words in a reggae, "|" character.
Sentence = ~ / word1 / word2 / >
You can convert word array into an appropriate regex with array.join Are ("|").
If words are likely to include regex metacharacters, then include each word in non-capturing brackets.
Sentence = ~ / (?: Word 1). (?: Word2). .... /
Using one of the Rijx should be more efficient than looping through the array of words, since the regex will be compiled into a single state statement.
Comments
Post a Comment