Answer the question
In order to leave comments, you need to log in
How to write a parser in LUA?
The task is to write a function for fast thinning of a text file in Lua.
html or javascript file. At the input we have string. You need to thin out a large block, for example like this:
function lua_filter_bloсk ("<-- Great work begin -->","<--Great work done-->","Shit happens")
function subs_filter_block ($start, $stop, $replace) {
global $result;
$start_pos = stripos ($result, $start);
if ($start_pos !== false) {
$end_pos = stripos($result, $stop);
if (($end_pos !== false) && ($start_pos < $end_pos)) {
$result =substr_replace ($result, $replace, $start_pos, ($end_pos - $start_pos + strlen($stop)));
}
}
return;
}
function subs_filter_block(startWord, endWord, replaceStr)
res = res:gsub(startWord .. "(.-)" .. endWord, replaceStr);
end
Answer the question
In order to leave comments, you need to log in
As far as I know, there is no non-exciting search, but in theory it should work :)
testStr = "Hello Johny !";
function do_replace(inputStr, startWord, endWord, replaceStr)
return string.gsub(inputStr, startWord .. " (%S+) " .. endWord, startWord .. " " .. replaceStr .. " " .. endWord);
end
result = do_replace(testStr, "Hello", "!", "habrahabr");
print(result);
Can be done using string.gsub. Something like string.gsub(givenString, start… '(.*)'… stop, replace)
This is in regex.
You can make the equivalent of the php code using the corresponding functions from the string module.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question