Search code examples
lualua-patterns

Lua variable in pattern match with special characters


I have a problem with a pattern match, when a variable with special characters in the value from regex is used in the match.

The code:

in_pic_file = 'C:\\Users\\marcus.LAPTOP-01\\RAW\\2021\\20211031\\20211031-_R4_1301.tif'
sync_dir_in = 'C:\\Users\\marcus.LAPTOP-01\\RAW'


in_pic_file_strip = string.match( in_pic_file, ''..sync_dir_in..'(.+)\\' )

print ( in_pic_file_strip )

The result I want to have is \2021\20211031 but I always get a nil. When i suggest LAPTOP_01 instead aof LAPTOP-01 then j get the expected result. Obviously the - singn is interpreted as a regex command. But how can I suppress this?


Solution

  • - is a magic character (zero or more, shortest match). If you want to match - you need to escape it with %.

    Same for . (any character). But . will not break your match. It will just allow any character instead of the . you're looking for.

    So instead of

    sync_dir_in = 'C:\\Users\\marcus.LAPTOP-01\\RAW'
    

    you need to use

    sync_dir_in = 'C:\\Users\\marcus%.LAPTOP%-01\\RAW'
    

    Lua 5.4 Reference Manual 6.4.1 Patterns

    %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 '%' to represent itself in a pattern.

    Allows us to write a simple helper function that escapes any non-alphanumeric character in our literal pattern.

    function escape_magic(pattern)
    
      return (pattern:gsub("%W", "%%%1"))
    
    end
    
    
    in_pic_file = 'C:\\Users\\marcus.LAPTOP-01\\RAW\\2021\\20211031\\20211031-_R4_1301.tif'
    sync_dir_in = 'C:\\Users\\marcus.LAPTOP-01\\RAW'
    
    
    in_pic_file_strip = string.match( in_pic_file, ''..escape_magic(sync_dir_in)..'(.+)\\' )
    
    print ( in_pic_file_strip )