Search code examples
stringluagsub

Lua string.gsub with a hyphen


I have two strings - each string has many lines like the following:

value_1 = "DEFAULT-VLAN"
value_2 = "WAN"
data = "HOSTNAME = DEFAULT-VLAN"
result = string.gsub(data,value_1,value_2)
print(result)

Result:

data = "HOSTNAME = DEFAULT-VLAN"

When the hyphen ("-") is deleted from the value it is working. Is there an easy way to solve this?


Solution

  • - is a magic character in Lua patterns. You need to escape it.

    Change

    value_1 = "DEFAULT-VLAN"
    

    to:

    value_1 = "DEFAULT%-VLAN"