当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby - Match unless "escape" character is present

问题描述:

I'm making a bot which gives full links for wikilinks like [[Foo]] given on IRC. What I want to do is to have '[[foo]]', 'before [[foo]], '[[foo]] after' and 'before [[foo]] after' match, but not '`[[foo]]', 'before `[[foo]]', etc., '`' being a sort of "escape" character in the program. So far I got

/[^`]\[\[(.+)\]\]/

However, that doesn't match "[[foo]]". If I add a ? after the negated character class, it defeats its purpose and all the backticked links still match.

How could I do this?

P.S. I want to have a single regex.

网友答案:

Just add the beginning of the line as an alternative to "a character that is not `":

/(?:[^`]|^)\[\[(.+)\]\]/.
分享给朋友:
您可能感兴趣的文章:
随机阅读: