[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Regular expression for matching lines
- From: Eike Decker <eike@...>
- Date: Mon, 30 Jul 2007 00:31:19 +0200
Zitat von "Thomas Harning Jr." <harningt@gmail.com>:
> On 7/26/07, Eike Decker <eike@cube3d.de> wrote:
> > Hi
> >
> > I am trying to find a regular expression that matches each line of an
> input
> > string but cannot find an expression that needs no additional checks. What
> I
> > have tried so far:
> >
> > for line in str:gmatch("[^\n]+") do
> > matches not empty lines
> >
> > for line in str:gmatch("([^\n]*)\n?") do
> > matches every line correctly BUT returns an empty string after the last
> > character, i.e.
> > "123" results in
> > line = "123"
> > line = ""
> >
> > I cannot get rid of the last match, so I wonder if there is an elegant
> > expression to match every line correctly, even empty lines and without
> adding
> > "virtual" lines.
> How about this this... it will give you every line...
>
> str:gmatch("([^\n]*)")
>
> No need to make it try to match the newline char... Not sure how it
> handles the end-of-string case however...
> --
> Thomas Harning Jr.
>
Won't work (tried it out) for the string
"1\n2\n345\n\n6789"
it will produce
1
2
345
6789
Every linebreak is captured as a zerolength string.
Eike