lua-users home
lua-l archive

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


> Why do I have a tab followed by 10 at the end of the string? (It is the
> number of matches...)

>From the gsub bit of the manual: "This function also returns, as a second
value, the total number of substitutions made".

So gsub is returning two values, and print displays both of them.
If you just want the string to be displayed then use an intermediate
variable:

  local val = gsub(...)
  print val

Try this if you don't want to match the spaces (which has to match at least
one digit):
  pattern = "(-?%d+)"

I'd also recommend that you escape any "magic" characters (see manual) - in
this instance your "-"
  pattern = "(%-?%d+)"

Cheers,

John
john@glowingslab.com


----- Original Message -----
From: "Philippe Lhoste" <PhiLho@gmx.net>
To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
Sent: 08 August 2001 09:08
Subject: RE: Syntax highlighting in / for Lua


> Nick Trout wrote:
> > Just an idea for code clarity:
> >
> > function makeDict(t)
> >   local d
> >   for i=1,getn(t) do d[ t[i] ]=1 end
> >   return d
> > }
> >
> > tokens = makeDict {
> >   "~=", "<=", ">=", "<", ">",
> >   "==", "=", "+", "-", "*",
> >   etc
> > }
> >
> > :-)
>
> Great, that's exactly the kind of feed-back I expect when posting code
here!
> I guess I am still doing some things Basic-like or C-like, but I am
getting
> better :-) I even briefly though of using tokens = { "~=", ... } and loop
on
> the elements!
>
> That's why I regulary ask (and give) Lua code: to see what solutions
others
> people have come with. Plus it gives usable code: I am using Eric Ries'
split
> function for parsing CGI requests.
>
> I like your solution because indeed it is clearer, and totally in the Lua
> spirit.
> Some tricks I will not use, because I think they hurt clarity. I saw a
> gsub(gsub(gsub(exp, ... ))) but even at the cost of speed (?) or memory
(??), I
> prefer to use: exp = gsub(exp, ... ) three times.
>
> I am still playing with my primitive syntax highlighting code (working
also
> on a more reliable one) for experimentation. I will add CGI support. I
don't
> use CGILua because I wanted to reinvent the wheel, looking at C and Perl
> codes. It is less efficient, but more informative.
>
> BTW, I have a question for Lua gurus: is it possible to match *any* legal
> (in Lua syntax) number using only *one* Lua regular expression? I am not
sure,
> since all elements are optionals, but at least some are required. But it
> won't be the first time I would be surprised by the users of this list...
>
> print(.02)
> print(0.)
> print(-.02)
> print(-.1E1)
> print(-.1e-1)
>
> The above are all legal (not "." nor ".e1"...).
> I am thinking of something like: -?%d*.?%d*[eE]?-?%d*
>
> Trying this expression (and simplier ones), I came up with a strange
result.
> At least one I don't understand.
>
> pNumbers = "1 4554 1. 12. .1 .01 .1E1 12E5 0.1e-1"
> nNumbers = "-1 -4554 -1. -12. -.1 -.01 -.1E1 -12E5 -0.1e-1"
> numbers = pNumbers .. nNumbers
> pattern = "(-?%d*.?%d*[eE]?-?%d*)"
> -- Ignore the above, for tests of the sought after RE...
>
> numbers = "1 22 333 4444 0 x 1 zz 55555"
> pattern = "(-?%d*)"
> print(gsub(numbers, pattern,
> function (n)
> return '<' .. n .. '>'
> end
> ))
>
> Output (Lua 4.0):
> <1><> <22><> <333><> <4444><> <0><> <>x<> <1><> <>z<>z<> <55555><> 19
>
> Why the spaces after the numbers and around the non-number letters are
> matched? Why do I have a tab followed by 10 at the end of the string? (It
is the
> number of matches...)
> I must miss something obvious...
>
> Regards.
>
> --
> --._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.--
> Philippe Lhoste (Paris -- France)
> Professional programmer and amateur artist
> http://jove.prohosting.com/~philho/
> --´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`--
>
> Sent through GMX FreeMail - http://www.gmx.net
>