Python:preg_replace函数模拟

2024-04-30 02:42:29 发布

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

我在PHP中有一个litle表达式:

  $search = array("'<(script|noscript|style|noindex)[^>]*?>.*?</(script|noscript|style|noindex)>'si",
    "'<\!--.*?-->'si",
    "'<[\/\!]*?[^<>]*?>'si",
    "'([\r\n])[\s]+'");

$replace = array ("",
  "",
  " ", 
  "\\1 ");

  $text = preg_replace($search, $replace, $this->pageHtml);

我是怎么在python上运行这个的?re.sub


Tags: textsearch表达式stylescriptthisarrayreplace
1条回答
网友
1楼 · 发布于 2024-04-30 02:42:29

作为@bereal commented使用正则表达式模块^{}

这里有一个简单的例子

Python:

>>> import re
>>> re.sub(r'([^A-Z])([A-Z])', r'\1_\2', 'camelCase').lower()
'camel_case'

为了好玩,在PHP上也有:

<?php
echo strtolower(preg_replace('/([^A-Z])([A-Z])/', '$1_$2', 'camelCase'));
// prints camel_case

相关问题 更多 >