Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gsub in Lua. Unable to replace pattern

I want to replace all phrases $br$ in the string for the character '\n'.

I write the following code: str = string.gsub("String 1 $br$ String 2", "$br$", "\n").

But this does not work and displays the string String 1 $br$ String 2. What am I doing wrong?

like image 912
Victor Avatar asked Jan 31 '15 21:01

Victor


People also ask

What does GSUB do in Lua?

gsub (s, pattern, repl [, n]) Returns a copy of s in which all (or the first n , if given) occurrences of the pattern have been replaced by a replacement string specified by repl , which can be a string, a table, or a function. gsub also returns, as its second value, the total number of matches that occurred.

Is GSUB slow?

#gsub is not only slower, but it also requires an extra effort for the reader to 'decode' the arguments.

How do I uninstall GSUB?

To remove a character in an R data frame column, we can use gsub function which will replace the character with blank. For example, if we have a data frame called df that contains a character column say x which has a character ID in each value then it can be removed by using the command gsub("ID","",as.


1 Answers

You need to escape the $ character since it represents the end of line.

str = string.gsub("String 1 $br$ String 2", "%$br%$", "\n")

If you want to grab the whitespace around $br$ as well:

str = string.gsub("String 1 $br$ String 2", "%s*%$br%$%s*", "\n")
like image 135
Ben Grimm Avatar answered Sep 24 '22 11:09

Ben Grimm