1)可以使用一些你正在解析的例子。
2)如果在表达式末尾使用“x”,可以在正则表达式中添加空格和注释,使其更易于理解
3)此外,通过分解它,您会注意到 ( ) 内的第二部分内容缺少数字匹配...而不是寻找 0 或多个“_”,并在看到数字时中断,因此不匹配。
while(<TOCFILE>)
{
$toc_line = $_;
$toc_line =~
s/ # replace the follwoing
<inlineFig # match this text
.*? # then any characters until the next sequence matches
( # throw the match into $1
\.\.\/pics\/ch09_inline99_ # ..\pics\cho9_inline99_
\d*?\.jpg # folowed by 0 or more numbers
)*? # keeping doing that until the next sequence matches
<\/inlineFig> # match this text
/ # with the follwoing
<img src="${1}" alt="" \/\> # some text and the result of $1 above.
/xg; # <- the x makes it ignore whitespace and #comments
$new_toc_file .= $toc_line;
}
4)如前所述,()*?只将最后一个匹配返回到 $1,但如果您的输入只是某种格式,这应该不是问题。