lua-users home
lua-l archive

Re: Regex question

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 18-Aug-05, at 6:26 PM, Aaron Brown wrote:
Keith Pimmel wrote:
I try anchoring it with the following
_,_,last = string.find(str,'(^\n]*$)')
which produces
nil
Try this:
_, _, last = string.find(str, '([^\n]*)$')
Lua (for some reason) doesn't like anchors to be inside
captures.
That will suffer from two problems:
First, if the string ends with \n, it will return an empty string, probably not what was desired. Second, it will be much slower than you might like; the pattern will be attempted at every character position in the string. If you knew that the string did not end with a \n, and that there was always at least two lines (or that the string did start with a \n), this would be quite a bit faster:
 string.find(str, '\n([^\n]+)$')
but I suspect that Shannon's for loop using string.gfind is actually the best solution.

AltStyle によって変換されたページ (->オリジナル) /