-2

我正在为 App Engine 上的基于 Python 的开源博客平台(类似于 WordPress)编写一个插件(带有 WebApp 框架和 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经验。

非常感谢!

4

1 回答 1

1

你试过什么?通过re手册。它有很多很好的信息,它会回答你可能遇到的许多问题。例如,re.escape用于使外部字符串安全。

于 2012-06-09T16:21:46.267 回答