【问题标题】:Rewrite PHP Regular Expression in Python在 Python 中重写 PHP 正则表达式
【发布时间】:2012-06-13 07:26:23
【问题描述】:

我正在为 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 经验。

非常感谢!

【问题讨论】:

  • 重写正则表达式的哪一部分遇到了麻烦?你试过什么?
  • @SusanMayer:你基本上是在要求我们为你做这件事,这在 StackOverflow 上是不受欢迎的。您需要先付出一些努力,否则您将得不到任何(或很少)帮助。也许您应该首先阅读PCRE (Perl-Compatible Regular Expressions) in PHP,或者尝试实现相同的功能,而不是通过逐个字母地重写 PHP 代码。与 Python 相比,PHP 有多个缺点,因此基于功能定义而不是实际实现可能是一个更好的主意。

标签: php python django google-app-engine webapp2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 2023-03-23
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多