简体   繁体   中英

Regex for matching word but not if it starts or ends with hyphen or colon

I've been having trouble figuring out the correct regex (in Ruby) for matching a word in a string, but not if the word starts or ends with a - or : .

I currently have:

/\bmatch this\b/i

The regex should match ( match this ) in:

 please match this!
 do not match this.

but not match:

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

I've tried:

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

I'd appreciate any help! I'm using the regex in Ruby, if it makes a difference.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM