[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: sscanf like in Lua
- From: steve donovan <steve.j.donovan@...>
- Date: Tue, 24 Aug 2010 09:46:50 +0200
2010/8/24 Michal Kolodziejczyk <miko@wp.pl>:
> You probably meant: (%-?%d), because you would be interested in at most
> one minus sign in the front of the number.
The task is sufficiently error-prone to benefit from a custom module:
http://stevedonovan.github.com/Penlight/#T35
"Simple Input Patterns" was my attempt at "Lua String Patterns for
Dummies" (in which category I put myself, most of the time)
> pat = sip.compile '$f / $f'"
> res = {}
> = pat('23 / -20',res)
true
> = res[1],res[2]
23 -20
It brings all the above elements to the party:
- spaces in the pattern are rendered as %s*
- $f is replaced by a floating-point number capture pattern
- any magic characters are escaped
- $f means that conversion to number must happen
steve d.