匹配单词的正则表达式,但不是以连字符或冒号开头或结尾

时间:2021-07-19 01:58:43

标签: regex ruby

我一直无法找出正确的正则表达式(在 Ruby 中)以匹配字符串中的单词,但如果单词以 -: 开头或结尾,则不会。

>

我目前拥有:

/\bmatch this\b/i

正则表达式应该匹配 (match this) 在:

 please match this!
 do not match this.

但不匹配:

do not :match this
do not -match this
do not match this:
do not match this-

我试过了:

/\b[^:-]match this[^:-]\b/i

我很感激任何帮助!我在 Ruby 中使用正则表达式,如果它有所不同的话。

1 个答案:

答案 0 :(得分:1)

我想我想通了,使用否定后视和否定前瞻

/(?<![:-])\bmatch this\b(?![:-])/i