Home > Enterprise >  Matching and replacing newline character using Lua pattern patch
Matching and replacing newline character using Lua pattern patch

Time:04-20

This is a follow up to a question I asked in the enter image description here

CodePudding user response:

You can use a function as a replacement argument:

result = string.gsub ( s, '\\sitem%s (.-)(\n\\)' , '\\item\\makefirstuc {%1},%2')

See the online demo.

Details:

  • \\sitem - a \sitem fixed string
  • %s - one or more whitespaces
  • (.-) - Group 1 (%1): any zero or more chars as few as possible
  • (\n\\) - Group 2 (%2): a newline and a \.

CodePudding user response:

The . pattern matcher in Lua's patterns matches new lines, so something like this would work:

s = s:gsub(
    "\\item%s (. )",
    function(it)
        return "\\item\\makefirstuc " .. it:gsub("\n", " ")
    end
)
  • Related