Question

In the following lua code:

function interp(s, tab)
  return (s:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end))
end

what does the %b mean?

and how does this match stuff like "${name}" ?

Was it helpful?

Solution

%bXY matches a sequence of characters that starts with X and ends with Y. Thus, %b{} matches {......} for any characters in between the braces.

The overall pattern in your example code first matches a $ character followed by a {, any number of characters, and then a }.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top