1. I've got the following test code:

     a = 'A_A';
     print(a:match('^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'));
    

    The printed result is nil. What am I doing wrong? This expression should mean that following strings should match:

     A
     A_B_C
     A_BBF_A12_13
    

    And following shouldn't:

     13AB
     ab
     AB__AB
     _AB
     AB_
    
  2. You have:

     (_[A-Z0-9]+)*
    

    That's not valid. You can't repeat a larger pattern, like that. So the way you're constructing the pattern is invalid right now.

    Remember that Lua patterns are not 'regular expressions'. They're a pattern matching language.

  3. Aw. So the way it should be done is 'splitting' the whole string onto smaller pieces and matching them independetly? Lua does not support normal regex functionality natively?

  4. No. The smallest known regular expressions engine is larger than the entire source of the Lua programming language and all of the standard libraries =) You can easily learn to work with Lua patterns, which are actually more powerful than regexp in some regards.

  5. Looks, like I've already learnt it as it's a subset of regex functionality. Or I don't understand something. So how my problem should be solved? Thanks.

  6. You haven't stated the problem, just given some examples, so I have no idea what you're trying to solve. It seems you have a restriction that you dont want two underscores next to each other. What are you trying to capture, and what are you trying to validate?

  7. I'm making an OOP-like class so I need to validate constant names: 1. It has to start with a letter. 2. All letters are uppercased. 3. It can contain letters, numbers and underscores. 4. Not more than one underscore can be in a row. 5. It can't end with underscore.

  8. I'm making an OOP-like class so I need to validate constant names: 1. It has to start with a letter. 2. All letters are uppercased. 3. It can contain letters, numbers and underscores. 4. Not more than one underscore can be in a row. 5. It can't end with underscore.

    I'd split it up into three patterns:

     function isValid(name)
       return name:match("^[A-Z][A-Z_0-9]*$") and not name:match("__") and name:match("[A-Z0-9]$")
     end
    
    1. Starts with a letter and contains a sequence of letters, numbers and undescores
    2. Cannot contain two underscores next to another
    3. Must end with a letter/number
  9. Thanks. Looks like I have to try to implement real regex in lua.

  10. No, that would be absolutely the wrong thing. Why can't you just do what I said? It works and it's clear.

    You could also structure your 'constant names' such that they're in a better format.

    Implementing regex in Lua just isn't going to happen and its the wrong solution. I'd like you to give me one reason why my solution isn't acceptable.

  11. It's obviously acceptible! But not that elegant and understandable. I'm a beginner to Lua or WoW programming but I have quite solid experience in PHP and JavaScript. Lua is quite similar to JavaScript though its OOP is implemented with metatables when JavaScript is handled with prototypes. In order to learn Lua (in context of WoW) I decided to make a framework something similar to Ext JS 4. The goal is to make programming more elegant and quick and get rid of XML at all. So I started from the bottom and currently working on implementation of the Array class (pure array with no non-integer indexes). My approach is to leave the main table empty while implement all logics in the protected metatable in order to achieve full control and some automation. In order to handle that I have to validate incoming keys in methods __index and __newindex. That's why I opened this topic. Sorry my bad english. :)

  12. It's obviously acceptible! But not that elegant and understandable.

    That depends on your definition of elegant or understandable. Regular expression are notoriously difficult for anyone to understand. Lua patterns, being a nice subset makes it easy to talk about them. My code is really easy to explain, as I did it. Yours was completely opaque initially.

    I'm a beginner to Lua or WoW programming but I have quite solid experience in PHP and JavaScript. Lua is quite similar to JavaScript though its OOP is implemented with metatables when JavaScript is handled with prototypes. In order to learn Lua (in context of WoW) I decided to make a framework something similar to Ext JS 4. The goal is to make programming more elegant and quick and get rid of XML at all.

    Don't need any libraries to do that, you can create everything in Lua that you can do in XML

    So I started from the bottom and currently working on implementation of the Array class (pure array with no non-integer indexes). My approach is to leave the main table empty while implement all logics in the protected metatable in order to achieve full control and some automation. In order to handle that I have to validate incoming keys in methods __index and __newindex. That's why I opened this topic. Sorry my bad english. :)

    Well, write a simple function to validate the keys, as I've done. No reason that shouldn't be sufficient for you.

    Writing regexp in Lua is just insane, however.

  13. That depends on your definition of elegant or understandable. Regular expression are notoriously difficult for anyone to understand. Lua patterns, being a nice subset makes it easy to talk about them. My code is really easy to explain, as I did it. Yours was completely opaque initially.

    May I disagree. I have read much bigger patterns that were written by someone else and had not much trouble with that. This pattern expresses the whole idea of the format of the string while small pieces are required to be gathered in one big picture first.

    Don't need any libraries to do that, you can create everything in Lua that you can do in XML

    Yes, I understand that. But people may choose to use XML because writing the following code can be a little bit not understandable and elegant:

     local a = createSomething();
     a.setSomething(...);
     a.setSomethingMore(...);
     b.attachSomewhere(a);
    

    While Ext JS approach can be really simple:

     b.add(
     {
       type:      'something',
       position: 'somewhere',
       param1:    '...',
       param2:    '...'
     });
    

    The main idea is fast configuration and possibility to incapsulate this configuration into reusable widget.

    Writing regexp in Lua is just insane, however.

    Or fun? :)

  14. That depends on your definition of elegant or understandable. Regular expression are notoriously difficult for anyone to understand. Lua patterns, being a nice subset makes it easy to talk about them. My code is really easy to explain, as I did it. Yours was completely opaque initially.

    May I disagree. I have read much bigger patterns that were written by someone else and had not much trouble with that. This pattern expresses the whole idea of the format of the string while small pieces are required to be gathered in one big picture first.

    You can disagree, but lots of people hold my opinion as well. I guess we won't convince each other.

    Don't need any libraries to do that, you can create everything in Lua that you can do in XML

    Yes, I understand that. But people may choose to use XML because writing the following code can be a little bit not understandable and elegant:

     local a = createSomething();
     a.setSomething(...);
     a.setSomethingMore(...);
     b.attachSomewhere(a);
    

    While Ext JS approach can be really simple:

     
     b.add(
     {
       type:      'something',
       position: 'somewhere',
       param1:    '...',
       param2:    '...'
     });
    

    The main idea is fast configuration and possibility to incapsulate this configuration into reusable widget.

    Writing regexp in Lua is just insane, however.

    Or fun? :)

    No. You really don't understand what you are proposing if you respond that way.

    Have fun!

  15. I really know what regex engine is about: parsing pattern string, iteration through internal states of parsed pattern object, iteration through position of a string, branching, backtracking, saving temporary captures and so on. But you should agree that this is much easier than making a compiler for a programming language for example. And this can be done either in Lua or in any other imperative programming language.

  16. I have no idea how to respond to any of that.