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
[^|\s] : left side of the substring is begining of string(^) or white space,
{substring} : subsring is matched exactly,
[\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
How to Create Wizard Forms in Ruby on Rails
Linkedin API for Company Directory
Get All Instance Variables Declared in Class
Is There a Way in Ruby 1.9 to Remove Invalid Byte Sequences from Strings
Does Ruby Call Initialize Method Automatically
How to Pass Data from a Controller to a Model with Ruby on Rails
Devise Login with User or Admin Models and Basecamp Style Subdomains
How to Make Httparty Ignore Ssl
How to Create an Anchor and Redirect to This Specific Anchor in Ruby on Rails
Check If a Constant Is Already Defined
Difference Between Resource and Resources in Rails Routing
Before/After Suite When Using Ruby Minitest
Rack::Request - How to Get All Headers
Rails Put Validation in a Module Mixin
Nested Forms in Rails - Accessing Attribute in Has_Many Relation