为什么我的替代品中的 $1 是空的? [英] Why is $1 empty in my substitution?

查看:37
本文介绍了为什么我的替代品中的 $1 是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将括号内的内容放入 img 标签中 src 属性的值中:

I am trying to put the stuff within parentheses into the value of a src attribute in an img tag:

while(<TOCFILE>)
{
    $toc_line = $_;
    $toc_line =~ s/<inlineFig.*?(\.\.\/pics\/ch09_inline99_*?\.jpg)*?<\/inlineFig>/<img src="${1}" alt="" \/\>/g;
    $new_toc_file .= $toc_line;
}

所以我希望在输出中看到这样的标签:

So I expected to see tags like this in the output:

<img src="../pics/ch09_inline99_00" alt="" />

但我得到的是:

<img src="" alt="" />

推荐答案

您的正则表达式中存在错误,因此该短语永远不会匹配任何内容:

There's an error in your regex so that phrase will never match anything:

inline99_*?\.jpg
        ^^^ 

我想你忘记了 \d 在星星前面,从你试图匹配的示例数据来看.

I think you forgot \d in front of the star, judging by the example data you are trying to match.

您甚至没有要求它会匹配,因为您在捕获的组之后放置了 *?.所以,它只是不匹配任何东西.所以这就是你得到的:什么都没有.

You're not even asking that it'll match, as you put a *? after the captured group. So, it just doesn't match anything. So that's what you get: nothing.

此外:

($PATTERN)*?

只会捕获它匹配的最后一个东西.这也可能不是你想要的.例如:

will only capture the last thing it matched. That probably isn't what you want, either. For example:

$_ = 'one two three';
s/(\w+\s*)*/$1/;
print;

打印三个".

这篇关于为什么我的替代品中的 $1 是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆