Using an escaped (maigc) character as boundary in a character range in Lua patterns









up vote
1
down vote

favorite












The Lua manual in section 6.4.1 on Lua Patterns states




A character class is used to represent a set of characters. The
following combinations are allowed in describing a character class:




  • x: (where x is not one of the magic characters ^$()%.*+-?) represents the character x itself.


  • .: (a dot) represents all characters.


  • %a: represents all letters.


  • %c: represents all control characters.


  • %d: represents all digits.


  • %g: represents all printable characters except space.


  • %l: represents all lowercase letters.


  • %p: represents all punctuation characters.


  • %s: represents all space characters.


  • %u: represents all uppercase letters.


  • %w: represents all alphanumeric characters.


  • %x: represents all hexadecimal digits.


  • %x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters.
    Any non-alphanumeric character (including all punctuation characters,
    even the non-magical) can be preceded by a % when used to represent
    itself in a pattern.


  • [set]: represents the class which is the union of all characters in set. A range of characters can be specified by separating the end
    characters of the range, in ascending order, with a -. All classes
    %x described above can also be used as components in set. All other
    characters in set represent themselves. For example, [%w_] (or
    [_%w]) represents all alphanumeric characters plus the underscore,
    [0-7] represents the octal digits, and [0-7%l%-] represents the
    octal digits plus the lowercase letters plus the - character.

You can put a closing square bracket in a set by positioning it as the
first character in the set. You can put a hyphen in a set by
positioning it as the first or the last character in the set. (You can
also use an escape for both cases.)



The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.



[^set]: represents the complement of set, where set is interpreted
as above.



For all classes represented by single letters (%a, %c, etc.), the
corresponding uppercase letter represents the complement of the class.
For instance, %S represents all non-space characters.



The definitions of letter, space, and other character groups depend on
the current locale. In particular, the class [a-z] may not be
equivalent to %l.
(Highlighting and some formatting added by me)




So, since the "interaction between ranges and classes is not defined.", how do you create a character class set that starts and/or ends with a (magic) character that needs to be escaped?



For example,



[%%-c]


does not define a character class that ranges from % to c and includes all characters in-between but a set that consists only of the three characters %, -, and c.










share|improve this question























  • This question came up recently in a closed and subsequently deleted post. However, I think this is an interesting question that deserves to be documented.
    – wp78de
    Nov 9 at 23:22














up vote
1
down vote

favorite












The Lua manual in section 6.4.1 on Lua Patterns states




A character class is used to represent a set of characters. The
following combinations are allowed in describing a character class:




  • x: (where x is not one of the magic characters ^$()%.*+-?) represents the character x itself.


  • .: (a dot) represents all characters.


  • %a: represents all letters.


  • %c: represents all control characters.


  • %d: represents all digits.


  • %g: represents all printable characters except space.


  • %l: represents all lowercase letters.


  • %p: represents all punctuation characters.


  • %s: represents all space characters.


  • %u: represents all uppercase letters.


  • %w: represents all alphanumeric characters.


  • %x: represents all hexadecimal digits.


  • %x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters.
    Any non-alphanumeric character (including all punctuation characters,
    even the non-magical) can be preceded by a % when used to represent
    itself in a pattern.


  • [set]: represents the class which is the union of all characters in set. A range of characters can be specified by separating the end
    characters of the range, in ascending order, with a -. All classes
    %x described above can also be used as components in set. All other
    characters in set represent themselves. For example, [%w_] (or
    [_%w]) represents all alphanumeric characters plus the underscore,
    [0-7] represents the octal digits, and [0-7%l%-] represents the
    octal digits plus the lowercase letters plus the - character.

You can put a closing square bracket in a set by positioning it as the
first character in the set. You can put a hyphen in a set by
positioning it as the first or the last character in the set. (You can
also use an escape for both cases.)



The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.



[^set]: represents the complement of set, where set is interpreted
as above.



For all classes represented by single letters (%a, %c, etc.), the
corresponding uppercase letter represents the complement of the class.
For instance, %S represents all non-space characters.



The definitions of letter, space, and other character groups depend on
the current locale. In particular, the class [a-z] may not be
equivalent to %l.
(Highlighting and some formatting added by me)




So, since the "interaction between ranges and classes is not defined.", how do you create a character class set that starts and/or ends with a (magic) character that needs to be escaped?



For example,



[%%-c]


does not define a character class that ranges from % to c and includes all characters in-between but a set that consists only of the three characters %, -, and c.










share|improve this question























  • This question came up recently in a closed and subsequently deleted post. However, I think this is an interesting question that deserves to be documented.
    – wp78de
    Nov 9 at 23:22












up vote
1
down vote

favorite









up vote
1
down vote

favorite











The Lua manual in section 6.4.1 on Lua Patterns states




A character class is used to represent a set of characters. The
following combinations are allowed in describing a character class:




  • x: (where x is not one of the magic characters ^$()%.*+-?) represents the character x itself.


  • .: (a dot) represents all characters.


  • %a: represents all letters.


  • %c: represents all control characters.


  • %d: represents all digits.


  • %g: represents all printable characters except space.


  • %l: represents all lowercase letters.


  • %p: represents all punctuation characters.


  • %s: represents all space characters.


  • %u: represents all uppercase letters.


  • %w: represents all alphanumeric characters.


  • %x: represents all hexadecimal digits.


  • %x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters.
    Any non-alphanumeric character (including all punctuation characters,
    even the non-magical) can be preceded by a % when used to represent
    itself in a pattern.


  • [set]: represents the class which is the union of all characters in set. A range of characters can be specified by separating the end
    characters of the range, in ascending order, with a -. All classes
    %x described above can also be used as components in set. All other
    characters in set represent themselves. For example, [%w_] (or
    [_%w]) represents all alphanumeric characters plus the underscore,
    [0-7] represents the octal digits, and [0-7%l%-] represents the
    octal digits plus the lowercase letters plus the - character.

You can put a closing square bracket in a set by positioning it as the
first character in the set. You can put a hyphen in a set by
positioning it as the first or the last character in the set. (You can
also use an escape for both cases.)



The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.



[^set]: represents the complement of set, where set is interpreted
as above.



For all classes represented by single letters (%a, %c, etc.), the
corresponding uppercase letter represents the complement of the class.
For instance, %S represents all non-space characters.



The definitions of letter, space, and other character groups depend on
the current locale. In particular, the class [a-z] may not be
equivalent to %l.
(Highlighting and some formatting added by me)




So, since the "interaction between ranges and classes is not defined.", how do you create a character class set that starts and/or ends with a (magic) character that needs to be escaped?



For example,



[%%-c]


does not define a character class that ranges from % to c and includes all characters in-between but a set that consists only of the three characters %, -, and c.










share|improve this question















The Lua manual in section 6.4.1 on Lua Patterns states




A character class is used to represent a set of characters. The
following combinations are allowed in describing a character class:




  • x: (where x is not one of the magic characters ^$()%.*+-?) represents the character x itself.


  • .: (a dot) represents all characters.


  • %a: represents all letters.


  • %c: represents all control characters.


  • %d: represents all digits.


  • %g: represents all printable characters except space.


  • %l: represents all lowercase letters.


  • %p: represents all punctuation characters.


  • %s: represents all space characters.


  • %u: represents all uppercase letters.


  • %w: represents all alphanumeric characters.


  • %x: represents all hexadecimal digits.


  • %x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters.
    Any non-alphanumeric character (including all punctuation characters,
    even the non-magical) can be preceded by a % when used to represent
    itself in a pattern.


  • [set]: represents the class which is the union of all characters in set. A range of characters can be specified by separating the end
    characters of the range, in ascending order, with a -. All classes
    %x described above can also be used as components in set. All other
    characters in set represent themselves. For example, [%w_] (or
    [_%w]) represents all alphanumeric characters plus the underscore,
    [0-7] represents the octal digits, and [0-7%l%-] represents the
    octal digits plus the lowercase letters plus the - character.

You can put a closing square bracket in a set by positioning it as the
first character in the set. You can put a hyphen in a set by
positioning it as the first or the last character in the set. (You can
also use an escape for both cases.)



The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.



[^set]: represents the complement of set, where set is interpreted
as above.



For all classes represented by single letters (%a, %c, etc.), the
corresponding uppercase letter represents the complement of the class.
For instance, %S represents all non-space characters.



The definitions of letter, space, and other character groups depend on
the current locale. In particular, the class [a-z] may not be
equivalent to %l.
(Highlighting and some formatting added by me)




So, since the "interaction between ranges and classes is not defined.", how do you create a character class set that starts and/or ends with a (magic) character that needs to be escaped?



For example,



[%%-c]


does not define a character class that ranges from % to c and includes all characters in-between but a set that consists only of the three characters %, -, and c.







lua lua-patterns






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 23:52

























asked Nov 9 at 23:21









wp78de

9,58651638




9,58651638











  • This question came up recently in a closed and subsequently deleted post. However, I think this is an interesting question that deserves to be documented.
    – wp78de
    Nov 9 at 23:22
















  • This question came up recently in a closed and subsequently deleted post. However, I think this is an interesting question that deserves to be documented.
    – wp78de
    Nov 9 at 23:22















This question came up recently in a closed and subsequently deleted post. However, I think this is an interesting question that deserves to be documented.
– wp78de
Nov 9 at 23:22




This question came up recently in a closed and subsequently deleted post. However, I think this is an interesting question that deserves to be documented.
– wp78de
Nov 9 at 23:22












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted











The interaction between ranges and classes is not defined.




Obviously, this is not a hard and fast rule (of regex character sets in general) but a Lua implementation decision. While using shorthand characters in character sets/ranges work in some (most) regex flavors, it does not in all (like in Python's re module, demo).



However, the second example is misleading:




Therefore, patterns like [%a-z] or [a-%%] have no meaning.




While the first example is fine since %a is a shorthand class (that represents all letters) in a set, [%a-z] is undefined and will return nil if matched against a string.



Escaped range characters in a [set]



In the second example, [a-%%], %% simply defines an escaped % sign and not a shorthand character class. The superficial problem is, the range is defined upsidedown, from high to low (in reference to the US ASCII value of the characters a 61 and % 37), e.g like an erroneous Lua pattern like [f-a]. If the set is defined in reverse order it seems to work: [%%-a] but all it does is matching the three individual characters instead of the range of characters between % and a; credit cyclaminist).



This could be considered a bug and, indeed, means it is not possible to create a range of characters in a [set] if one of the defining range characters need to be escaped.



Possible Solution



Start the character range from the next character that does not need to be escaped - and then add the remaining escaped characters individually, e.g.



[%%&-a]


Sample:



for w in string.gmatch("%&*()-0Aa", "[%%&-a]") do
print(w)
end


This is the answer I have found. Still, maybe somebody else has something better.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234492%2fusing-an-escaped-maigc-character-as-boundary-in-a-character-range-in-lua-patte%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted











    The interaction between ranges and classes is not defined.




    Obviously, this is not a hard and fast rule (of regex character sets in general) but a Lua implementation decision. While using shorthand characters in character sets/ranges work in some (most) regex flavors, it does not in all (like in Python's re module, demo).



    However, the second example is misleading:




    Therefore, patterns like [%a-z] or [a-%%] have no meaning.




    While the first example is fine since %a is a shorthand class (that represents all letters) in a set, [%a-z] is undefined and will return nil if matched against a string.



    Escaped range characters in a [set]



    In the second example, [a-%%], %% simply defines an escaped % sign and not a shorthand character class. The superficial problem is, the range is defined upsidedown, from high to low (in reference to the US ASCII value of the characters a 61 and % 37), e.g like an erroneous Lua pattern like [f-a]. If the set is defined in reverse order it seems to work: [%%-a] but all it does is matching the three individual characters instead of the range of characters between % and a; credit cyclaminist).



    This could be considered a bug and, indeed, means it is not possible to create a range of characters in a [set] if one of the defining range characters need to be escaped.



    Possible Solution



    Start the character range from the next character that does not need to be escaped - and then add the remaining escaped characters individually, e.g.



    [%%&-a]


    Sample:



    for w in string.gmatch("%&*()-0Aa", "[%%&-a]") do
    print(w)
    end


    This is the answer I have found. Still, maybe somebody else has something better.






    share|improve this answer
























      up vote
      1
      down vote



      accepted











      The interaction between ranges and classes is not defined.




      Obviously, this is not a hard and fast rule (of regex character sets in general) but a Lua implementation decision. While using shorthand characters in character sets/ranges work in some (most) regex flavors, it does not in all (like in Python's re module, demo).



      However, the second example is misleading:




      Therefore, patterns like [%a-z] or [a-%%] have no meaning.




      While the first example is fine since %a is a shorthand class (that represents all letters) in a set, [%a-z] is undefined and will return nil if matched against a string.



      Escaped range characters in a [set]



      In the second example, [a-%%], %% simply defines an escaped % sign and not a shorthand character class. The superficial problem is, the range is defined upsidedown, from high to low (in reference to the US ASCII value of the characters a 61 and % 37), e.g like an erroneous Lua pattern like [f-a]. If the set is defined in reverse order it seems to work: [%%-a] but all it does is matching the three individual characters instead of the range of characters between % and a; credit cyclaminist).



      This could be considered a bug and, indeed, means it is not possible to create a range of characters in a [set] if one of the defining range characters need to be escaped.



      Possible Solution



      Start the character range from the next character that does not need to be escaped - and then add the remaining escaped characters individually, e.g.



      [%%&-a]


      Sample:



      for w in string.gmatch("%&*()-0Aa", "[%%&-a]") do
      print(w)
      end


      This is the answer I have found. Still, maybe somebody else has something better.






      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted







        The interaction between ranges and classes is not defined.




        Obviously, this is not a hard and fast rule (of regex character sets in general) but a Lua implementation decision. While using shorthand characters in character sets/ranges work in some (most) regex flavors, it does not in all (like in Python's re module, demo).



        However, the second example is misleading:




        Therefore, patterns like [%a-z] or [a-%%] have no meaning.




        While the first example is fine since %a is a shorthand class (that represents all letters) in a set, [%a-z] is undefined and will return nil if matched against a string.



        Escaped range characters in a [set]



        In the second example, [a-%%], %% simply defines an escaped % sign and not a shorthand character class. The superficial problem is, the range is defined upsidedown, from high to low (in reference to the US ASCII value of the characters a 61 and % 37), e.g like an erroneous Lua pattern like [f-a]. If the set is defined in reverse order it seems to work: [%%-a] but all it does is matching the three individual characters instead of the range of characters between % and a; credit cyclaminist).



        This could be considered a bug and, indeed, means it is not possible to create a range of characters in a [set] if one of the defining range characters need to be escaped.



        Possible Solution



        Start the character range from the next character that does not need to be escaped - and then add the remaining escaped characters individually, e.g.



        [%%&-a]


        Sample:



        for w in string.gmatch("%&*()-0Aa", "[%%&-a]") do
        print(w)
        end


        This is the answer I have found. Still, maybe somebody else has something better.






        share|improve this answer













        The interaction between ranges and classes is not defined.




        Obviously, this is not a hard and fast rule (of regex character sets in general) but a Lua implementation decision. While using shorthand characters in character sets/ranges work in some (most) regex flavors, it does not in all (like in Python's re module, demo).



        However, the second example is misleading:




        Therefore, patterns like [%a-z] or [a-%%] have no meaning.




        While the first example is fine since %a is a shorthand class (that represents all letters) in a set, [%a-z] is undefined and will return nil if matched against a string.



        Escaped range characters in a [set]



        In the second example, [a-%%], %% simply defines an escaped % sign and not a shorthand character class. The superficial problem is, the range is defined upsidedown, from high to low (in reference to the US ASCII value of the characters a 61 and % 37), e.g like an erroneous Lua pattern like [f-a]. If the set is defined in reverse order it seems to work: [%%-a] but all it does is matching the three individual characters instead of the range of characters between % and a; credit cyclaminist).



        This could be considered a bug and, indeed, means it is not possible to create a range of characters in a [set] if one of the defining range characters need to be escaped.



        Possible Solution



        Start the character range from the next character that does not need to be escaped - and then add the remaining escaped characters individually, e.g.



        [%%&-a]


        Sample:



        for w in string.gmatch("%&*()-0Aa", "[%%&-a]") do
        print(w)
        end


        This is the answer I have found. Still, maybe somebody else has something better.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 23:36









        wp78de

        9,58651638




        9,58651638



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234492%2fusing-an-escaped-maigc-character-as-boundary-in-a-character-range-in-lua-patte%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus