Finding exact words in a string

10,540

Solution 1

The canonical regex way of doing this is to search on word boundaries:

pry(main)> "foo/womens/bar".match(/\bwomens\b/)
=> #<MatchData "womens">
pry(main)> "foo/womens/bar".match(/\bmens\b/)
=> nil
pry(main)> "foo/mens/bar".match(/\bmens\b/)
=> #<MatchData "mens">
pry(main)> "foo/mens/bar".match(/\bwomens\b/)
=> nil

That said, either splitting, or searching with the leading "/", may be adequate.

Solution 2

If you first check for women it should work:

# assumes str is not nil
def gender(str)
  if str.include?("women")
    "F"
  elsif str.include?("men") 
    "M"
  else
    nil
  end
end

If this is not what you are looking for, please explain your problem in more detail.

Solution 3

You could split with / and check for string equality on the component(s) you want -- no need for a regex there

Share:
10,540
tob88
Author by

tob88

Updated on June 04, 2022

Comments

  • tob88
    tob88 almost 2 years

    I have a list of links to clothing websites that I am categorising by gender using keywords. Depending on what website they are for, they all have different URL structures, for example...

    www.website1.com/shop/womens/tops/tshirt

    www.website2.com/products/womens-tshirt

    I cannot use the .include? method because regardless of whether it is .include?("mens") or .include?("womens"), it will return true. How can I have a method that will only return true for "womens" (and vice versa). I suspect it may have to be some sort of regex, but I am relatively inexperienced with these, and the different URL structures make it all the more tricky. Any help is much appreciated, thanks!

  • Paul.s
    Paul.s over 12 years
    Is the double negation really required? I use it in statically typed languages just wondering what the argument is for it here?
  • Yule
    Yule over 12 years
    Double negative turns everything into true or false. so nil or false becomes false, everything else becomes true. OP requires a boolean value to be returned
  • Paul.s
    Paul.s over 12 years
    I know how double negation works. The OP doesn't state a strict requirement for a boolean type and in Ruby everything that isn't nil or false is true. I was just wondering if there was any other arguments for using it.
  • Yule
    Yule over 12 years
    Nope, just the sentence "will only return true for "womens"" by OP. Returing 21 is not "true". It's not necessary if you're then using it as a boolean expression but not sure how its used going forward
  • tob88
    tob88 over 12 years
    This is a smart way of doing this that I had not thought of. I am using Dave Newtons method because it is more succinct, but I have given you an upvote for special commendation.
  • curot
    curot almost 8 years
    For the record, word characters don't include things like dashes or underscores, and thus those count as word boundaries, so if your string has those in it this approach won't exactly work.