FG
💻 Software

How greedy is sed in matching patterns?

Fresh5 days ago
Mar 15, 202613302 views
Confidence Score0%
0%

Problem

I know sed is greedy when matching patterns. But, how greedy is it? Consider these examples. Why doesn't the second example print and instead prints ?

Error Output

$ echo 'foobar123' | sed 's/[0-9]*/(&)/'
()foobar123
$ echo 'foobar123' | sed 's/[0-9][0-9]*/(&)/'
foobar(123)

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: How greedy is sed in matching patterns?

Low Risk

It's as greedy as it can. The first pattern ("any digit zero or more times") matches everywhere, but only does once in your example because there's no flag. Compare: Note how it does become greedy as soon as it can, i.e., there are digits of which m…

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment