[英]perl regular expression to substitute
在html文件中,我希望在這些模式中添加[]中數字的鏈接:[1]或[1-2]或[1,3,6]或[1,3,4-6,9 ]。 我知道如何匹配第一個簡單的:
$html =~ s`\[\d+\]`\[<a href="#$1"><$1>\]`g;
其余模式怎么樣?
另外,對於這種模式:[1],我想只查找一個在[和]之間的數字,忽略模式匹配和替換中的任何其他內容,因為在我的html代碼中,[]中的數字可能有所不同與他們相關的HTML代碼。
謝謝!
更新
算法:
use Data::Dumper;
# input
my $html = "[1],[1-2],[1,3,6],[1,3,4-6],[5]";
# result array
my @result;
# recursive way to generate html link tag
sub asHtmlLink($) {
return "<a href=\"\#$_[0]\">$_[0]</a>";
}
# seperate by ,
foreach (split(',',$html)) {
# match pattern
m/(\d+)(\-)?(\d+)?/g;
# if there is a digit after a minus
if (defined $3) {
push(@result,asHtmlLink($1).$2.asHtmlLink($3));
} else {
push(@result,asHtmlLink($1));
}
}
# dump
print Dumper @result;
結果:
$VAR1 = '<a href="#1">1</a>';
$VAR2 = '<a href="#1">1</a>-<a href="#2">2</a>';
$VAR3 = '<a href="#1">1</a>';
$VAR4 = '<a href="#3">3</a>';
$VAR5 = '<a href="#6">6</a>';
$VAR6 = '<a href="#1">1</a>';
$VAR7 = '<a href="#3">3</a>';
$VAR8 = '<a href="#4">4</a>-<a href="#6">6</a>';
$VAR9 = '<a href="#5">5</a>';
正則表達式鏈接: Regex101
use strict;
use Carp::Assert 'assert';
sub replace {
my $html = shift;
# first expand subexpressions of the for "n-m"; (will fail if
# n > m !)
1 while $html =~ s{
(\[\s*(?:\s*\d\s*,\s*)*) # prefix (1)
(\d+)\s*-\s*(\d+) # target (2, 3)
(?=[^\]]*\]) # suffix (lookahead)
}
{assert($2 <= $3),
$1 . join(", ", $2..$3)}ex;
# now replace individual integers with hyperlinks
1 while $html =~ s{
(\[\s*(?:\s*\d+\s*,\s*)*) # prefix (1)
(\d+) # target (2)
(?=[^\]]*\]) # suffix (lookahead)
}
{$1\n<a href="#$2">$2</a>}x; # \n added to make
# output easier to
# to read; it's safe
# to omit it
return $html;
}
一些非正式測試:
for my $h ( '[1, 3-7, 10-11 ]',
'[ 0]',
'[1 ]',
'[ ]',
'[ 9-19]',
'[9-9]',
'[19-9]',
'[19-z]'
) {
my $r = eval { replace($h); };
if ( $@ ) {
$r = $@;
}
printf "%s\n%s\n\n", $h, $r;
}
輸出:
[1, 3-7, 10-11 ]
[
<a href="#1">1</a>,
<a href="#3">3</a>,
<a href="#4">4</a>,
<a href="#5">5</a>,
<a href="#6">6</a>,
<a href="#7">7</a>,
<a href="#10">10</a>,
<a href="#11">11</a> ]
[ 0]
[
<a href="#0">0</a>]
[1 ]
[
<a href="#1">1</a> ]
[ ]
[ ]
[ 9-19]
[
<a href="#9">9</a>,
<a href="#10">10</a>,
<a href="#11">11</a>,
<a href="#12">12</a>,
<a href="#13">13</a>,
<a href="#14">14</a>,
<a href="#15">15</a>,
<a href="#16">16</a>,
<a href="#17">17</a>,
<a href="#18">18</a>,
<a href="#19">19</a>]
[9-9]
[
<a href="#9">9</a>]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.