为什么默认情况下正则表达式会贪婪? [英] Why are regular expressions greedy by default?

查看:105
本文介绍了为什么默认情况下正则表达式会贪婪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于初学者编写正则表达式,这似乎是一个巨大的困惑源,它可能导致隐藏的性能问题,而且似乎典型的用例是非贪婪的.

It seems that this is a huge source of confusion for beginners writing regular expressions, can cause hidden performance problems, and it would seem that a typical use case would be non-greedy.

这是仅出于遗留原因(它是如何首先完成的,每个实现都对此进行复制),还是有原因?

Is this just for legacy reasons (it was how it was first done, and every implementation copies that), or is there a reason for it?

推荐答案

歇斯底里的葡萄干


答案的一部分可能涉及实际计算中RE的起源.它们最初是从自动机理论和形式语言理论开始的理论概念,直到 ed( 1).

Hysterical Raisens


Part of the answer may involve the origins of REs in practical computing. They were originally a theoretical concept from automata theory and formal language theory until Ken Thompson himself wrote a real implementation and used them in qed and ed(1).

原始版本只有贪婪的语法,因此甚至没有决定.

The original version had only the greedy syntax and so there wasn't a decision to even make.

这篇关于为什么默认情况下正则表达式会贪婪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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