我正在App Engine上为基于Python的开源博客平台(类似于WordPress)编写一个插件(带有WebApp Framework和Django模板)
这个插件和这个完全一样:http://wordpress.org/extend/plugins/blog-mechanics-keyword-link-plugin-v01/
一个允许您定义关键字/链接对的插件。关键词 会自动链接到您的每个帖子中。
以下是关键的正则表达式源代码:
// The regular expression comes from an older
// auto link plugin by Sean Hickey. It fixed the autolinking inside a link
// problem. Thanks to [Steph] for the code.
// For keywords with quotes (') to work, we need to disable word boundary matching
if ($ignorecase) $case = "i"; else $case="";
$cleankeyword = preg_quote($cleankeyword,'''');
if (BM_KEYWORDLINK_QUOTES && strpos( $cleankeyword , '''')>0)
$regEx = '''(?!((<.*?)|(<a.*?)))(' . $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))''s' . $case;
else
$regEx = '''(?!((<.*?)|(<a.*?)))('b'. $cleankeyword . ''b)(?!(([^<>]*?)>)|([^>]*?</a>))''s' . $case;
$content = preg_replace($regEx,$url,$content,$limit);
如何在 Python 中重写正则表达式?我没有PHP经验。
多谢!
你试过什么?浏览re
手册。它有很多很好的信息,它将回答您可能遇到的许多问题。例如,re.escape
用于使外部字符串安全。