Search code examples
regexlualua-patterns

Lua pattern similar to regex positive lookahead?


I have a string which can contain any number of the delimiter §\n. I would like to remove all delimiters from a string, except the last occurrence which should be left as-is. The last delimiter can be in three states: \n, §\n or §§\n. There will never be any characters after the last variable delimiter.

Here are 3 examples with the different state delimiters:

abc§\ndef§\nghi\n
abc§\ndef§\nghi§\n
abc§\ndef§\nghi§§\n

I would like to remove all delimiters except the last occurrence.

So the result of gsub for the three examples above should be:

abcdefghi\n
abcdefghi§\n
abcdefghi§§\n

Using regular expressions, one could use §\\n(?=.), which matches properly for all three cases using positive lookahead, as there will never be any characters after the last variable delimiter.

I know I could check if the string has the delimiter at the end, and then after a substitution using the Lua pattern §\n I could add the delimiter back onto the string. That is however a very inelegant solution to a problem which should be possible to solve using a Lua pattern alone.

So how could this be done using a Lua pattern?


Solution

  • str:gsub( '§\\n(.)', '%1' ) should do what you want. This deletes the delimiter given that it is followed by another character, putting this character back into to string.

    Test code

    local str = {
        'abc§\\ndef§\\nghi\\n',
        'abc§\\ndef§\\nghi§\\n',
        'abc§\\ndef§\\nghi§§\\n',
    }
    
    for i = 1, #str do
        print( ( str[ i ]:gsub( '§\\n(.)', '%1' ) ) )
    end
    

    yields

    abcdefghi\n
    abcdefghi§\n
    abcdefghi§§\n