用HTML标签替换符号

时间:2011-02-05 23:41:17

标签: regex perl

我想用HTML标签替换符号。例如:

This :is: some :text: that could also look :a: bit :different:.

应该成为

This <span id="selected">is</span> some <span id="selected">text</span> that could also look <span id="selected">a</span> bit <span id="selected">different</span>.

3 个答案:

答案 0 :(得分:2)

$s =~ s{:([^:]+):}{<span id="selected">$1</span>}g;

答案 1 :(得分:1)

$str = "This :is: some :text: that could also look :a: bit :different:";
$str =~ s/:(\w+):/<span id='selected'>$1<\/span>/g;
print $str;

答案 2 :(得分:1)

(:)(.+?)\1

使用\1将允许您将“:”更改为其他字符,并确保结束匹配。