How to Match Full Words and Not Substrings in Ruby

How to match full words and not substrings in Ruby

The | operator in regex takes the widest scope possible. Your original regex matches either \ba or an or all\b.

Change the whole regex to:

/\b(?:#{stopwordlist})\b/

or change stopwordlist into a regex instead of a string.

stopwordlist = /a|an|all/

Even better, you may want to use Regexp.union.

Regex to match exact word in string

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($).

Regex for a whole word in a string, that doesn't start with the dollar sign

Your \b(?![$])a\b (and /\b(?!\$)a\b/ is an equivalent expression) matches a a in $a because the pattern matches any a enclosed with non-word characters or start/end of string and that is not $ (and a cannot be $, thus the lookahead is always true here). The \b(?=[^$])a\b regex again matches an a in $a because this expression matches any a as a whole word (i.e. enclosed with non-word characters or start/end string) that is not equal to $ (the (?=[^$]) positive lookahead requires the next character to be anything but $, and as a is not $, the pattern returns a in $a).

Assuming you want to match a word a that is not preceded with $, use

'b a b' =~ /\b(?<!\$)a\b/

The negative lookbehind (?<!\$) will fail a match if the whole word a is preceded with $ symbol.

See Ruby demo:

puts 'b a b'.gsub(/\b(?<!\$)a\b/, "B")  # b B b
puts 'b $a b'.gsub(/\b(?<!\$)a\b/, "B") # b $a b

Ruby - replace words in a string if instances do not match

This is a gsub that I think will do what you want:

string = 'richard julie richard julie sam letty sam letty'

string.gsub /\b(?!(richard|julie))\w+/, 'richard'

\b is a word break and ?!(richard|julie) is a look ahead for not richard nor julie. So this is basically a match for a word break and the letters that follow it, where those letters don't match richard nor julie

ruby enclose selected whole words in brackets

It's just a string substitution using a regular expression. You can use the word boundary special character to prevent it from matching your parameter when it's in the middle of another word. And put your method inside the String class so that you can call it directly on a string like in your example.

class String
def enclose_in_brackets(selection)
self.gsub(/(\b#{selection}\b)/i, '{\1}')
end
end

'Sometimes some stupid people say some stupid words'.enclose_in_brackets('some')
# Sometimes {some} stupid people say {some} stupid words.

How to check whether a string contains a substring in Ruby

You can use the include? method:

my_string = "abcdefg"
if my_string.include? "cde"
puts "String includes 'cde'"
end


Related Topics



Leave a reply



Submit