2 one-file pure-lua 5.1 regex library
4 This library _is_ the simple way to add utf8 support into your application.
6 Some examples from http://www.lua.org/manual/5.1/manual.html#5.4 :
8 local utf8 = require "utf8"
10 local s = "hello world from Lua"
11 for w in string.gmatch(s, "%a+") do
21 s = "Привет, мир, от Lua"
22 for w in utf8.gmatch(s, "[^%p%d%s%c]+") do
33 s = "from=world, to=Lua"
34 for k, v in string.gmatch(s, "(%w+)=(%w+)") do
37 for k,v in pairs(t) do
47 for k, v in utf8.gmatch(s, "([^%p%s%c]+)=([^%p%s%c]+)") do
50 for k,v in pairs(t) do
58 local x = string.gsub("hello world", "(%w+)", "%1 %1")
60 --hello hello world world
62 x = utf8.gsub("Ahoj světe", "([^%p%s%c]+)", "%1 %1")
64 --Ahoj Ahoj světe světe
66 x = string.gsub("hello world", "%w+", "%0 %0", 1)
70 x = utf8.gsub("Ahoj světe", "[^%p%s%c]+", "%0 %0", 1)
74 x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
76 --world hello Lua from
78 x = utf8.gsub("γεια κόσμο από Lua", "([^%p%s%c]+)%s*([^%p%s%c]+)", "%2 %1")
82 Notice, there are some classes that can work only with latin(ASCII) symbols,
83 for details see: https://github.com/Stepets/utf8.lua/blob/master/utf8.lua#L470
85 Of course you can do this trick:
87 for k,v in pairs(utf8) do
91 But this can lead to very strange errors. You were warned.
93 A little bit more interesting examples:
95 local utf8 = require 'utf8'
96 for k,v in pairs(utf8) do
100 local str = "пыщпыщ ололоо я водитель нло"
101 print(str:find("(.л.+)н"))
102 -- 8 26 ололоо я водитель
104 print(str:gsub("ло+", "보라"))
105 -- пыщпыщ о보라보라 я водитель н보라 3
107 print(str:match("^п[лопыщ ]*я"))