I need to look for all occurrences of a word in a line, but the search must be case insensitive. What else do I need to add to my regular expression?
arr = line.scan(/\s+#{word}\s+/)
You need modifier /i
arr = line.scan(/\b#{word}\b/i)
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm
And better to use \b
for word boundaries, because the second \s+
in your regex eats spaces, which may be used for the first \s+
of another matched word; also your regex fails on the beginning and the end of line:
> "asd asd asd asd".scan /\s+asd\s+/
=> [" asd "]
> "asd asd asd asd".scan /\basd\b/
=> ["asd", "asd", "asd", "asd"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With