Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lua gsub %b <-- how does this work?

Tags:

gsub

lua

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}" ?

like image 645
anon Avatar asked Jul 17 '26 21:07

anon


1 Answers

%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 }.

like image 186
Amber Avatar answered Jul 21 '26 11:07

Amber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!