Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何搜索和替换特定的src=";url";使用perl在html中标记?_Html_Regex_Perl - Fatal编程技术网

如何搜索和替换特定的src=";url";使用perl在html中标记?

如何搜索和替换特定的src=";url";使用perl在html中标记?,html,regex,perl,Html,Regex,Perl,假设我有一个变量,其中包含一堆文本,包括普通HTML标记中的URL。特别是,我对标记的src=元素感兴趣。假设我知道我想在那堆文本中搜索的确切的src=字符串,我想用其他文本替换它。以下是我尝试过的一些内容(伪代码): my$bunchott=new( 字符串=>$bunchotxt ); while(my$tag=$parser->get_tag('img')){ #按原样打印$tag->,“\n”; 对于我的$attr(qw(src)){ $replaceStr=sprintf qq{%s=

假设我有一个变量,其中包含一堆文本,包括普通HTML标记中的URL。特别是,我对标记的src=元素感兴趣。假设我知道我想在那堆文本中搜索的确切的src=字符串,我想用其他文本替换它。以下是我尝试过的一些内容(伪代码):

my$bunchott=new(
字符串=>$bunchotxt
);
while(my$tag=$parser->get_tag('img')){
#按原样打印$tag->,“\n”;
对于我的$attr(qw(src)){
$replaceStr=sprintf qq{%s=“%s”\n},$attr,$tag->get_attr($attr);
$parsedtag=~s/“//g;
my@bits=$url->path_segments();
$cidreplace{$unparsedtag}=$path;
}
my$replaceStr=“src:\”已替换\”;
$bunchotxt=~s/$findURL/$replaceStr/g;
打印“$buchotxt\n”;
}

首先,我们需要将您的问题归结为我们真正关心的部分。您的示例代码不太好,因为它包含许多不相关的错误,所以我可以随意删除一些我认为解决问题绝对不必要的内容。我还为您的HTML添加了一些换行符,以帮助解决h水平滚动

这就给我们留下了这样一个问题:

use strict;
use warnings;

use HTML::TokeParser::Simple;

my $bunchotxt = << 'END_MESSAGE';
<a href="http://link.com/image.gif">
  <img
      class="alignleft size-thumbnail wp-image-295"
      src="http://link.com/image.gif"
      alt="shredding"
      width="150"
      height="150" />
</a>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis convallis
fringilla dui eget cursus. Nullam in mauris viverra elit pharetra fringilla.
Pellentesque gravida ligula sit amet magna blandit, semper luctus enim semper.
Nam a sem ut ex aliquam consectetur. Nulla enim metus, porta at elementum non,
facilisis ullamcorper nisl. Vestibulum sed iaculis ante. Nullam mollis luctus
posuere.

Suspendisse ipsum odio, iaculis in malesuada id, varius
END_MESSAGE

my $parser = HTML::TokeParser::Simple->new(string => $bunchotxt);

while (my $tag = $parser->get_tag('img')) {
    my $src = $tag->get_attr('src');
    $bunchotxt =~ s/\Qsrc="$src"\E/src:"replaced"/g;
    print "$bunchotxt\n";
}
使用严格;
使用警告;
使用HTML::TokeParser::Simple;
我的$bunchotxt=new(字符串=>$bunchotxt);
while(my$tag=$parser->get_tag('img')){
my$src=$tag->get_attr('src');
$bunchott=~s/\Qsrc=“$src”\E/src:“已替换”/g;
打印“$bunchotxt\n”;
}
结果的第一行是:

<a href="http://link.com/image.gif"><img class="alignleft size-thumbnail wp-image-295" src:"replaced" ...

然后您只需要匹配您想要匹配的字符串。您忽略了原始源中的
字符。您在搜索模式中的第一个双引号位于错误的位置。另外,你不需要转义
=
,但在这种情况下,你需要转义
,因为你想匹配一个文字点。伙计,我希望我能以真正应该问的方式重新发布这个问题。在代码中,我试图找到工作,$findstr字符由另一个循环填充,该循环使用HTML::Tokeparser::Simple从原始文本中获取所有src=元素的列表。因此,$findstr最终拥有从代码的解析器部分返回的值。我试着用quotemeta()来逃避这一点,但这似乎没有任何区别。希望你能在知道这一点的情况下再次参与进来。好吧,考虑到我刚才的评论,我修改了上面的代码。希望有人愿意在这个问题上再尝试一次。你的例子中仍然有一些无关的废话,实际上并没有说明问题所在!例如,
$findURL
来自哪里?这很重要,因为这是问题的关键。另外,您说过您试图使用
quotemeta
,但我在您的代码中没有看到它。请学习如何创建。这是一个很好的解决方案。经过进一步的研究,我发现了一个语法分析器的set_attr。这实际上也很有效,但我无法看到如何将更改提交回字符串。这100%有效。感谢您耐心地对待我公认的匆忙样本代码。@2crd3o替换中的
src:
也是一个本应是
src=
的打字错误吗?