stringsplitluaemojilua-5.1

How to separate emojies in string without spaces in LUA 5.1


I want to use split function of LUA 5.1 to split string of emoji characters without spaces and add space between ones, but I can't do it rightly. So I do it by this way, but it's wrong:

#!/usr/bin/env lua

local text = "๐Ÿ‘Š๐Ÿฝ๐Ÿ‘Š๐Ÿฝ๐ŸฅŠ๐Ÿ˜‚๐ŸŽน๐Ÿ˜‚๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘จโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆโŒšโ†”"
for emoji in string.gmatch(text, "[%z\1-\127\194-\244][\128-\191]*") do
    io.write(emoji .. " ")          
end

See in browser Firefox 65!

MY WRONG RESULT: ๐Ÿ‘Š ๐Ÿฝ ๐Ÿ‘Š ๐Ÿฝ ๐ŸฅŠ ๐Ÿ˜‚ ๐ŸŽน ๐Ÿ˜‚ ๐Ÿ‘ฉ ๐Ÿ‘ฉ ๐Ÿ‘ง ๐Ÿ‘ฆ ๐Ÿ‘จ ๐Ÿ‘ฆ ๐Ÿ‘ฆ โŒš โ†”

WAITED RESULT: ๐Ÿ‘Š๐Ÿฝ ๐Ÿ‘Š๐Ÿฝ ๐ŸฅŠ ๐Ÿ˜‚ ๐ŸŽน ๐Ÿ˜‚ ๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ ๐Ÿ‘จโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ โŒš โ†”


Solution

  • local text = "๐Ÿ‘Š๐Ÿฝ๐Ÿ‘Š๐Ÿฝ๐ŸฅŠ๐Ÿ˜‚๐ŸŽน๐Ÿ˜‚๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘จโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆโŒšโ†”"
    for emoji in text
       :gsub("(.)([\194-\244])", "%1\0%2")
       :gsub("%z(\240\159\143[\187-\191])", "%1")
       :gsub("%z(\239\184[\128-\143])", "%1")
       :gsub("%z(\226\128\141)%z", "%1")
       :gmatch"%Z+" 
    do
       print(emoji)
    end