0

What is the efficient and straightforward code for finding array elements in a string. For example:

a = Array['aaa', 'bbb', 'ccc', 'ddd', 'eee']

b = "This is the sample string with aaa , blah blah"

c = someFunction(b, a)
puts c
=> ['aaa']

Suppose a array have 100 elements, I want to know which of array element is found in the string. I should match exacct word. So xbbb, bbaa, ... not matched.

8
  • It looks a bit unclear what you want to solve? Also please add what you already tried so far? Commented Mar 28, 2020 at 20:20
  • Do you want to know the index of 'aaa'? Commented Mar 28, 2020 at 20:32
  • @Mark Yeah, I want to know the index of aaa or its value. Commented Mar 28, 2020 at 20:40
  • 1
    The easiest way to clarify your question is to show the desired return value for you example (as a valid Ruby object). That's something that should always done when an example is given. You may wish to include more than one element of a in your string b. Also, please make clear whether "xbbb", if it were in the string, would be a match. Commented Mar 28, 2020 at 20:42
  • 1
    I suggest you state that in the question. Questions should be self-contained, in part because not all readers read all the comments. Commented Mar 28, 2020 at 20:48

4 Answers 4

5

I think this is one of the possible solutions:

def some_method(string, array)
  string.split & array
end

a = Array['aaa', 'bbb', 'ccc', 'ddd', 'eee']
b = "This is the sample string with aaa , blah blah"
> some_method(b, a)
=> ['aaa']

a = Array['aaa', 'bbb', 'ccc', 'ddd', 'eee']
b = "This is the sample string with xaaa , blah blah"
> some_method(b, a)
=> []
Sign up to request clarification or add additional context in comments.

1 Comment

If performance for larger arrays ever comes up, it might be worth looking into Set. Sets have a faster lookup time, and therefore should in theory have a faster intersection result.
1
def find_elements(my_string, my_array)
  my_string.split & my_array
end 

You can split the string into an array and then find the intersection of both arrays using & or even intersection if you are on ruby 2.7. This will return an array containing all of the unique matching elements.

Comments

1

One way I found is like below -

array =  Array['aaa', 'bbb', 'ccc', 'ddd', 'eee']
string = "This is the sample string with aaa , blah blah"
found = []

array.each { |a| found << a if string.include? a }

puts found
=> ["aaa"] 

EDIT

After knowing another use case where it is needed exact match and as include? matches 'aaa' even if it is in 'xxaaa', one possible solution is using Set Intersection with Arrays in Ruby -

def some_methodd(array, string)
  string.split & array
end

Then it will return the exact match.

=> ["aaa"]

2 Comments

Since you posted your answer the OP has clarified that "aaa" should not be matched if the string ended "...string with xaaa , blah blah".
Thanks a lot for pointing out. Had no idea that include? has this edge case. I am grateful to you.
1

You can also use #select with a regular expression to determine which array elements are in the string.

def check_string(ary, str)
  ary.select do |e| 
    str =~ /\b#{e}\b/ 
  end
end

p check_string(%w(aaa bbb ccc), 'Here is a saaample bbb string ccc') # => ['bbb', 'ccc']

This gives you a lot of flexibility as to what matches and what doesn't, since if you want to change that, all you have to do is change the regex. This example assumes that you want whole word matches with words in an array.

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.