Perl 不会更新到下一场比赛 [英] Perl doesn't update to next match

查看:29
本文介绍了Perl 不会更新到下一场比赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道这是怎么发生的,但似乎我的 perl 正则表达式匹配在匹配后不会更新到下一个匹配.而不是更新 $&和 $1 变量与每个匹配,它卡在第一个.

Not idea how this is happening, but it seems my perl regular expression matches do not update to the next match, after doing a match. Instead of updating the $& and $1 variables with each match, it gets stuck in the first one.

我到处寻找,发现这非常令人沮丧.

I've looker everywhere and found this extremely frustrating.

请参阅下面调试器的输出,如您所见,第一个匹配项有意义,但第二个匹配项没有意义.

See output from debugger below, as you can see, the first match makes sense, but the second one doesn't.

谢谢

  DB<79>  $riz =~ m{url=(.*?)Support};

  DB<80> p$&;
url="http://www.svartapelsin.se" draggingName="Bunny Camp Support
  DB<81> $riz =~ m{href=(.*artist?)};

  DB<82> p $&;
url="http://www.svartapelsin.se" draggingName="Bunny Camp Support
  DB<83>

<小时>

更新:这是另一个示例,显示文本artist"在字符串中,但仍未找到.$riz 的值是一个巨大的 HTML 代码,所以很难发布.


Update: Here's another sample showing that the text "artist" is in the string, but it is still not finding it. The value of $riz is a huge HTML code, so it is hard to post.

DB<103> $riz =~ m{url=(.*?)Support};

  DB<104> p $&;
url="http://www.svartapelsin.se" draggingName="Bunny Camp Support
  DB<105> $riz =~ m{artist};

  DB<106> p $&;
url="http://www.svartapelsin.se" draggingName="Bunny Camp Support
  DB<107> p  string.index($riz,"artist");
string105
  DB<108>

<小时>

我的 $riz 是此链接中的所有 HTMLhttp://itunes.apple.com/us/app/id385972277

当您使用用户代理时iTunes/10.2 (Macintosh; U; PPC Mac OS X 10.2)

When you use the user agent iTunes/10.2 (Macintosh; U; PPC Mac OS X 10.2)

这是另一个具有相同 $riz 的示例

Here's another example with the same $riz

  DB<128>  $riz =~ m/.*/;

  DB<129> p $&;
url="http://www.svartapelsin.se" draggingName="Bunny Camp Support
  DB<130>
...
    DB<136> p substr $riz,0,20;
    <?xml version="1.0"
      DB<137>

我的意思是,这不是很荒谬吗?它应该只输出 $riz 的值吗?正如您所看到的,这与显示的内容不同.另外,m/.*/怎么可能不是有效的正则表达式?

I mean, isn't this just ridiculous? it should've just outputted the value of $riz no? Which as you can see is different form what is shown. Also, how could m/.*/ not be a valid regex?

推荐答案

perldebug这样说

调试器无法识别的任何命令都直接执行(eval'd) 作为当前包中的 Perl 代码.

Any command not recognized by the debugger is directly executed (eval'd) as Perl code in the current package.

请注意,所述 eval 受隐式范围的约束.结果任何新引入的词法变量或任何修改的捕获缓冲区评估后内容丢失.调试器是一个很好的环境学习 Perl,但如果您使用以下材料进行交互实验应该在同一个范围内,把它塞进一行.

Note that the said eval is bound by an implicit scope. As a result any newly introduced lexical variable or any modified capture buffer content is lost after the eval. The debugger is a nice environment to learn Perl, but if you interactively experiment using material which should be in the same scope, stuff it in one line.

因此 $&$1 等变量在调试器命令的执行过程中被本地化,并在命令完成后丢失.

So the $& and $1 etc. variables are localised during execution of a debugger command, and are lost one the command completes.

你可以使用

$riz =~ m{url=(.*?)Support}; print $&, "\n"; print $1, "\n";

$riz =~ m{url=(.*?)Support}; ($and, $one) = ($&, $1);
p $and
p $one

但是没有任何东西可以在同一命令行中保留这些值,一旦正则表达式比较完成,它们就会永远丢失.

but without something to preserve those values in the same command line they are forever lost once the regex comparison completes.

这篇关于Perl 不会更新到下一场比赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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