2

I've looked around but haven't been able to find a working solution to my problem.

I have an array of two strings input and want to test which element of the array contains an exact substring Test.

One thing I have tried (among numerous other attempts):

input = ["Test's string", "Test string"]
# Alternative input array that it needs to work on:
#  ["Testing string", "some Test string"]
substring = "Test"
if (input[0].match(/\b#{substring}\b/))
  puts "Test 0 "
  # Do something...
elsif (input[1].match(/\b#{substring}\b/))
  puts "Test 1"
  # Do something different...
end

The desired result is a print of "Test 1". The input can be more complex but overall I am looking for a way to find an exact match of a substring in a longer string. I feel like this should be a rather trivial regex but I haven't been able to come up with the correct pattern. Any help would be greatly appreciated!

7
  • @nhahtdh if you have a better solution I'm open to it. My actual case is more complex then I made it appear to be here. I attempted to simplify it as much as possible.
    – jkeuhlen
    Commented Dec 8, 2014 at 6:55
  • @RustamA.Gasanov Unfortunately that won't work as my case is a little more complex. For example, input[1] can contain more than just the single word Test
    – jkeuhlen
    Commented Dec 8, 2014 at 6:56
  • Then could you provide this complex case? No matter how much words input[1] contains as long as you are looking for exact strings correspondence Commented Dec 8, 2014 at 7:00
  • 1
    Your pattern is correct then and since your code is working, you probably want to ask for codereview Commented Dec 8, 2014 at 7:17
  • 1
    It seems to work in some cases but not in others. I admit, I never executed the code exactly as written. In one of my cases, the input[0] string contains something similar to Test's which will yield a false positive and send me into "Test 0"
    – jkeuhlen
    Commented Dec 8, 2014 at 7:20

3 Answers 3

3

Following code may be what you are looking for.

input = ["Testing string", "Test string"]
substring = "Test"

if (input[0].match(/[^|\s]#{substring}[\s|$]/)
  puts "Test 0 "
elsif (input[1].match(/[^|\s]#{substring}[\s|$]/)
  puts "Test 1"
end

The meaning of the pattern /[^|\s]#{substring}[\s|$]/ is

  1. [^|\s] : left side of the substring is begining of string(^) or white space,

  2. {substring} : subsring is matched exactly,

  3. [\s|$] : right side of the substring is white space or end of string($).

4
  • I changed my question according to the change of your question.
    – Fumu 7
    Commented Dec 8, 2014 at 7:38
  • This didn't exactly solve my problem but it put me on the write track to generating my pattern. Thanks!
    – jkeuhlen
    Commented Dec 8, 2014 at 19:38
  • jkeuhlen, you should learn the way to express what is your problem!
    – Fumu 7
    Commented Dec 9, 2014 at 4:17
  • Yeah I realize that this wasn't very clear. I oversimplified what my actual problem was and I didn't phrase my question correctly at all. Thanks for trying to help!
    – jkeuhlen
    Commented Dec 9, 2014 at 4:20
2

One way to that is as follows:

input = ["Testing string", "Test"]

"Test #{ input.index { |s| s[/\bTest\b/] } }"
  #=> "Test 1"

input = ["Test", "Testing string"]
"Test #{ input.index { |s| s[/\bTest\b/] } }"
  #=> "Test 0"

\b is the regex denotes a word boundary.

Maybe you want a method to return the index of the first element of input that contains the word? That could be:

def matching_index(input, word)
  input.index { |s| s[/\b#{word}\b/i] }
end

input = ["Testing string", "Test"]   
matching_index(input, "Test")    #=> 1
matching_index(input, "test")    #=> 1
matching_index(input, "Testing") #=> 0
matching_index(input, "Testy")   #=> nil

Then you could use it like this, for example:

word = 'Test'
puts "The matching element for '#{word}' is at index #{ matching_index(input, word) }"
  #=> The matching element for 'Test' is at index 1

word = "Testing"
puts "The matching element for '#{word}' is '#{ input[matching_index(input, word)] }'"
  #The matching element for 'Testing' is 'Testing string'
5
  • Sorry I guess my question wasn't quite as clear as I thought. I don't want to ONLY print out "Test 0" or "Test 1" I want to actually do things in each of those cases. I wrote it that way to just simplify my condition.
    – jkeuhlen
    Commented Dec 8, 2014 at 7:00
  • I guess you need to clarify then. "The desired result is a print of "Test 1" seems pretty clear. @AvinashRaj, thanks for the catch. Commented Dec 8, 2014 at 7:03
  • Edited the question some. I had hoped the prints weren't seen as the end goal of my code but I guess I was mistaken!
    – jkeuhlen
    Commented Dec 8, 2014 at 7:05
  • Again, I'm not looking for an index. I need to check for occurrences of a substring within a larger string and then execute code in one or another conditional statement.
    – jkeuhlen
    Commented Dec 8, 2014 at 7:17
  • Yes, but if you have the index i, the element (string) of input containing the word is input[i]. Right? If you just want the string, rather than the index, then input.find { |s| s[/\b#{word}\b/i] } does it. Commented Dec 8, 2014 at 7:23
0

The problem is with your bounding. In your original question, the word Test will match the first string because the ' is will match the \b word boundary. It's a perfect match and is responding with "Test 0" correctly. You need to determine how you'll terminate your search. If your input contains special characters, I don't think the regex will work properly. /\bTest my $money.*/ will never match because the of the $ in your substring.

What happens if you have multiple matches in your input array? Do you want to do something to all of them or just the first one?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.