Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lua variable in pattern match

Im just wondering if it is possible to put a variable in a pattern match in Lua. Like something similar to the following:

var = "hello"
pattern = string.match(datasource, "(var)%s(a%+)")

The reason I need to do this is because the variable "var" will change periodically. (it will be in a loop)

Cheers in advance

like image 919
greatodensraven Avatar asked Jul 14 '11 11:07

greatodensraven


1 Answers

Lua doesn't handle string interpolation inside of the quotes. Instead, you'll need to concatenate the parts with the var as a var reference and the rest as quote strings.

"("..var..")%s(a%+)" starts with a "(" as a string literal, concatenates the variable, then finishes off the rest of the string with a string literal.

like image 95
Tom Avatar answered Oct 17 '22 10:10

Tom