我该如何解决我的正则表达式与贪婪的量词不太匹配的问题? [英] How can I fix my regex to not match too much with a greedy quantifier?

查看:85
本文介绍了我该如何解决我的正则表达式与贪婪的量词不太匹配的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一行:

"14:48 say;0ed673079715c343281355c2a1fde843;2;laka;hello ;)"

我使用一个简单的正则表达式对此进行解析:

I parse this by using a simple regexp:

if($line =~ /(\d+:\d+)\ssay;(.*);(.*);(.*);(.*)/) {
    my($ts, $hash, $pid, $handle, $quote) = ($1, $2, $3, $4, $5);
}

但是;最后把事情搞砸了,我不知道为什么.贪婪的运算符不应该处理一切"吗?

But the ; at the end messes things up and I don't know why. Shouldn't the greedy operator handle "everything"?

推荐答案

贪婪的运算符会尝试捕获尽可能多的内容,并且仍然与字符串匹配.发生了什么事,第一个(在"say"之后)抓住了"0ed673079715c343281355c2a1fde843; 2",第二个抓住了"laka",第三个抓住了"hello",第四个找到了括号.

The greedy operator tries to grab as much stuff as it can and still match the string. What's happening is the first one (after "say") grabs "0ed673079715c343281355c2a1fde843;2", the second one takes "laka", the third finds "hello " and the fourth matches the parenthesis.

您需要做的是使除最后一个以外的所有内容都不是贪婪的,因此它们要尽可能少地抓取并仍然匹配字符串:

What you need to do is make all but the last one non-greedy, so they grab as little as possible and still match the string:

(\d+:\d+)\ssay;(.*?);(.*?);(.*?);(.*)

这篇关于我该如何解决我的正则表达式与贪婪的量词不太匹配的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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