如何让正则表达式从字符串的开头开始 [英] How to get a regex to start from the beginning of a string

查看:151
本文介绍了如何让正则表达式从字符串的开头开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我遇到的一个奇怪的问题(可能以前见过但从未注意过).

This is an oddball issue I've encountered (and probably have seen before but never paid attention to).

代码要点如下:

my $url = 'http://twitter.com/' . $handle;
my $page = get($url);

if($page =~ m/Web<\/span>\s*<a href=\"(.+?)\"/gi) {
    $website = $1;
}

if($page =~ m/follower_count\" class=\"stats_count numeric\">(.+?)\s*</g) {
    $num_followers = $1;
}

它获取一个 twitter url 并执行一些正则表达式来捕获关注者的数量和用户的网站.这段代码实际上工作正常.但是,当您在搜索关注者之后切换顺序并搜索网站时,网站显示为空.事实证明,当你正则表达式一个字符串时,它似乎保存了最后一次匹配的位置.在 html 中,关注者计数在网站显示后出现.如果您首先执行关注者计数正则表达式,就像它启动了关注者计数停止的网站正则表达式(如对字符串的索引引用).

It gets a twitter url and does a bit of regex to capture the # of followers and the website of the user. This code actually works fine. But when you switch the order and search for the website AFTER you search for follower, website comes up empty. As it turns out, when you regex a string, it seems to sort of save the location of where that last match was made. In the html, the follower count comes up after the website display. If you do the follower count regex first, it's like it starts up the website regex where the follower count left off (like an index reference to the string).

让我感到困惑的是,我在末尾有g"运算符,表示全局",如全局搜索字符串......从头开始".

What has me baffled is that i have the "g" operator at the end, signifying "global", as in "search the string globally... from the beginning".

我在这里遗漏了什么吗?我似乎无法弄清楚为什么它会恢复字符串上的最后一个正则表达式位置(如果有意义的话).

Am I missing something here? I can't seem to figure out why it's resuming the last regex position on the string (if that makes sense).

推荐答案

/g 修饰符,在标量上下文中,不会像你想象的那样做.摆脱它.

The /g modifier, in scalar context, doesn't do what you think it does. Get rid of it.

正如 perlretut 解释的那样,/g 在标量上下文中依次循环每个匹配.它是为循环使用而设计的,如下所示:

As perlretut explains, /g in scalar context cycles over each match in turn. It's designed for use in a loop, like so:

while ($str =~ /pattern/g) {
    # match on each occurence of 'pattern' in $str in turn
}

另一种使用 /g 的方法是在列表上下文中:

The other way to use /g is in list context:

my @results = $str =~ /pattern/g; # collect each occurence of 'pattern' within $str into @results

如果您在标量上下文中使用 /g 并且没有对其进行迭代,那么您几乎肯定没有正确使用它.

If you're using /g in scalar context and you're not iterating over it, you're almost certainly not using it right.

这篇关于如何让正则表达式从字符串的开头开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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