[thelist] Grep Question - match multiple lines but not greedy

Scott Wehrenberg swehren at gmail.com
Mon May 9 11:22:12 CDT 2005


On 5/6/05, Erik Heerlein <erik at erikheerlein.com> wrote:
> I can match line 1 and line 30 separately but I don't know how to match
> the stuff in between without being greedy and matching the whole file.
> I'd like to say, "Find a block of text where the first line is the
> date, followed by some text I don't care about and the last line is the
> Subtotal with the price that I do care about. Or to put it another way:

Syntax (and support) depends mostly on which tool you're using to do
the regex, but generally speaking simply appending a question mark
after your wildcard will make it non-greedy. So you probably want
something like this:

Date: May 5, 2005.*?Subtotal[ ]+= \$([0-9]+\.00)

The "*?" should match the smallest possible number of occurences to
make the regex work.


More information about the thelist mailing list