用Python重写PHP正则表达式

2024-10-01 07:34:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个基于Python的开源博客平台(类似于WordPress)在appengine上(使用WebApp框架和Django模板)编写一个插件

这个插件和这个插件完全一样:http://wordpress.org/extend/plugins/blog-mechanics-keyword-link-plugin-v01/

A plugin that allows you to define keyword/link pairs. The keywords are automatically linked in each of your posts.

以下是关键的正则表达式源代码:

// 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的经验。在

非常感谢!在


Tags: theto插件iflinkcontentpluginkeyword
1条回答
网友
1楼 · 发布于 2024-10-01 07:34:20

您尝试了什么?仔细阅读re手册。它有很多很好的信息,它将回答你可能有的许多问题。例如,re.escape使外部字符串安全。在

相关问题 更多 >