0

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?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Marcus J
  • 23
  • 5

1 Answers1

2

- 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 )
Piglet
  • 27,501
  • 3
  • 20
  • 43
  • Thank you for this hint. Is there a way to protect these magic characters by a command. Something that adds the % in front of every magick character? Because the values of the both variables depends on useraction. – Marcus J Dec 27 '21 at 12:46
  • see edit for a conventient way to escape magic characters – Piglet Dec 27 '21 at 13:01