我一直无法找出正确的正则表达式(在 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 中使用正则表达式,如果它有所不同的话。
答案 0 :(得分:1)
我想我想通了,使用否定后视和否定前瞻
/(?<![:-])\bmatch this\b(?![:-])/i