开发者

Regex: Finding specific word, excluding ones surrounded with a special char/tag

开发者 https://www.devze.com 2023-03-23 10:15 出处:网络
I want to find a specific word in a text, but not the ones that surrounded with a special char, or tag, etc.

I want to find a specific word in a text, but not the ones that surrounded with a special char, or tag, etc.

For example:

This word should be find, but [this word] shouldn't.

Is it possible to do this with Regular Expression? Or I should thi开发者_StackOverflownk about writing a special parser?

Thanks.


Try the following

(^|[^\[])[Tt]his word($|[^$\]])

(^|[^\[]) -> line start or everything except an opening square bracket

[Tt]his word -> make the first letter case insensitive

($|[^\]]) -> line end or everything except an closing square bracket

0

精彩评论

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