开发者

Regex that match each word in a string regardless of space or hyphen?

开发者 https://www.devze.com 2023-03-23 21:47 出处:网络
I thought this question could be interesting to put it up here. For example I have a string for search query like: \"hello wor开发者_开发知识库ld\"

I thought this question could be interesting to put it up here.

For example I have a string for search query like: "hello wor开发者_开发知识库ld"

There could be strings like this:

Hello World
Hello-world
hello!! world

How could you write an expression that will dynamically match these? If the expression works, it should works on this too:

Search: "Hi pals!"

Hi pals
Hi! Pals!
Hi-pals

Is it possible?


Sure, you could do something like this:

/hi.*?pals/i

the trailing i at the end enables case-insensitive matching, and the .*? matches any characters (except line breaks) between the "hi" and "pals".

A Rubular demo: http://rubular.com/r/cwtbV2iTwy

Beware that it also matches a string like "Himalaya pals":

Himalaya pals
^^       ^^^^

If you don't want that to happen, add some word-boundaries in the mix:

/\bhi\b.*?\bpals\b/i


Presumably you don't want "hi pals" to match "chi palsy", either, right? Seems like you're trying to find strings that have the same words as your input. If so, here's a way:

class String
  def words
    scan(/\w+/).map(&:downcase)
  end
end

a = ["Hi pals", "Hi, pals", "hi-pals", "hi! ? pals?", "hipals", "himalaya pals"]
search = "hi pals"

a.select {|test| test.words == search.words}

=> ["Hi pals", "Hi, pals", "hi-pals", "hi! ? pals?"]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号