Search code examples
regexlanguage-designregex-greedy

Why are regular expressions greedy by default?


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?


Solution

  • 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.