ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Regular expressions in Lua.
@ 2012-12-15  0:12 Andre Caldas
  2012-12-15  0:57 ` Hans Hagen
  0 siblings, 1 reply; 5+ messages in thread
From: Andre Caldas @ 2012-12-15  0:12 UTC (permalink / raw)
  To: ConTeXt users

Hi!

I have to use pattern matching in lua. I tried the function "string.gmatch".
I want to match (iterate over) strings of the type
([0-9]+)(-([0-9]*))?(,|$)

For example:
1,3- (iterations: 1 and 3-)
1-3,6,7 (iterations: 1-3 then 6 then 7)
1-2,5-6,10- (iterations 1-2 then 5-6 then 10-)


André Caldas.
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Regular expressions in Lua.
  2012-12-15  0:12 Regular expressions in Lua Andre Caldas
@ 2012-12-15  0:57 ` Hans Hagen
  2012-12-15  1:10   ` Andre Caldas
  0 siblings, 1 reply; 5+ messages in thread
From: Hans Hagen @ 2012-12-15  0:57 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On 12/15/2012 1:12 AM, Andre Caldas wrote:
> Hi!
>
> I have to use pattern matching in lua. I tried the function "string.gmatch".
> I want to match (iterate over) strings of the type
> ([0-9]+)(-([0-9]*))?(,|$)

> For example:
> 1,3- (iterations: 1 and 3-)
> 1-3,6,7 (iterations: 1-3 then 6 then 7)
> 1-2,5-6,10- (iterations 1-2 then 5-6 then 10-)

function whatever(str,n,action)
     action = action or print
     for s in string.gmatch(str,"[^,]+") do
         local a, b, c = string.match(s,"^(%d+)(%-?)(%d-)$")
         if c ~= "" then
             for i=tonumber(a),tonumber(c) do
                 action(i)
             end
         elseif b ~= "" then
             for i=tonumber(a),n or tonumber(a) do
                 action(i)
             end
         elseif a then
             action(tonumber(a))
         end
     end
end

whatever("1,3-",5,function(i) print(">>>",i) end)
whatever("1-3,6,7")
whatever("1-2,5-6,10-",30)


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Regular expressions in Lua.
  2012-12-15  0:57 ` Hans Hagen
@ 2012-12-15  1:10   ` Andre Caldas
  2012-12-15  1:47     ` Hans Hagen
  0 siblings, 1 reply; 5+ messages in thread
From: Andre Caldas @ 2012-12-15  1:10 UTC (permalink / raw)
  To: Hans Hagen; +Cc: mailing list for ConTeXt users

Hello, Hans!

I had just managed to do it. I will switch to yours instead of mine...
I don't have any experience in Lua. Sorry for bothering with such easy
requests...

By the way, simplesteps recognizes number ranges! :-)
https://bitbucket.org/andrecaldas/context-simplesteps/src/1c7ced45208b78289590008760daa3bac05ec503/mod/tex/context/third/simplesteps/simplesteps.lua?at=default#cl-37


Thank you,
André Caldas.
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Regular expressions in Lua.
  2012-12-15  1:10   ` Andre Caldas
@ 2012-12-15  1:47     ` Hans Hagen
  2012-12-15 12:00       ` Andre Caldas
  0 siblings, 1 reply; 5+ messages in thread
From: Hans Hagen @ 2012-12-15  1:47 UTC (permalink / raw)
  To: Andre Caldas; +Cc: mailing list for ConTeXt users

On 12/15/2012 2:10 AM, Andre Caldas wrote:
> Hello, Hans!
>
> I had just managed to do it. I will switch to yours instead of mine...
> I don't have any experience in Lua. Sorry for bothering with such easy
> requests...

As ranges are sometimes handy I've added a parser to the core (no upload 
yet, will happen sometime next week)

-- utilities.parsers.stepper("1,3-",5,function(i) print(">>>",i) end)
-- utilities.parsers.stepper("1-3,6,7")
-- utilities.parsers.stepper("1-3,6,7",function(i) print(">>>",i) end)
-- utilities.parsers.stepper("1:3,6,7")
-- utilities.parsers.stepper(" 1 : 3, ,7 ")
-- utilities.parsers.stepper("1-2,5-6,25-*",30)

So:

- * as optional final value symbol
- optional spaces around : and, and around list
- - as well as :

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Regular expressions in Lua.
  2012-12-15  1:47     ` Hans Hagen
@ 2012-12-15 12:00       ` Andre Caldas
  0 siblings, 0 replies; 5+ messages in thread
From: Andre Caldas @ 2012-12-15 12:00 UTC (permalink / raw)
  To: Hans Hagen; +Cc: mailing list for ConTeXt users

Hi!

> As ranges are sometimes handy I've added a parser to the core

Nice. I will use it as soon as it is available.
It is a very good thing that range specifications will be uniform
across different modules.


André Caldas.
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-12-15 12:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-15  0:12 Regular expressions in Lua Andre Caldas
2012-12-15  0:57 ` Hans Hagen
2012-12-15  1:10   ` Andre Caldas
2012-12-15  1:47     ` Hans Hagen
2012-12-15 12:00       ` Andre Caldas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).