github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/stringlib/lua/matching.lua (about) 1 local function errtest(f) 2 return function(...) 3 local ok, err = pcall(f, ...) 4 if ok then 5 print"OK" 6 else 7 print(err) 8 end 9 end 10 end 11 12 do 13 local function pf(...) 14 print(string.find(...)) 15 end 16 17 pf("a", "a", 1, true) 18 --> =1 1 19 20 pf("hello world!", "o w", 1, true) 21 --> =5 7 22 23 pf("xyzt", "yt", 1, true) 24 --> =nil 25 26 pf("1234 abc453", "%l+") 27 --> =6 8 28 29 pf(" foo=[a [lovely] day];", "(%w+)=(%b[])") 30 --> =3 22 foo [a [lovely] day] 31 32 print(pcall(string.find, "abc", "(xx%1)")) 33 --> ~false\t.* 34 35 local errf = errtest(string.find) 36 37 errf(1) 38 --> ~2 arguments needed 39 40 errf("x", "y", "z") 41 --> ~must be an integer 42 43 pf("x", "x", 3) 44 --> =nil 45 46 pf("x", "x", -10) 47 --> =1 1 48 49 pf("x", "y") 50 --> =nil 51 end 52 53 do 54 local function pm(...) 55 print(string.match(...)) 56 end 57 58 pm("Let me *stress* that I *am*", "%*.-%*") 59 --> =*stress* 60 61 pm("Let me *stress* that I *am*", "%*.-%*", 17) 62 --> =*am* 63 64 pm("Let me *stress* that I *am*", "%*(.-)%*") 65 --> =stress 66 67 pm("Let me *stress* that I *am*", "%*(.-)%*", 17) 68 --> =am 69 70 pm("A *bold* and an _underline_", "([*~_])(.-)%1") 71 --> =* bold 72 73 pm("A *b_o_l_d* and an _under~line_", "([*~_])(.-)%1") 74 --> =* b_o_l_d 75 76 pm("A *b_o_l_d* and an _under~line_", "([*~_])(.-)%1", 10) 77 --> =_ under~line 78 79 pm("abcd", "b", -100) 80 --> =b 81 82 local errm = errtest(string.match) 83 84 errm("x") 85 --> ~2 arguments needed 86 87 errm("x", "%") 88 --> ~malformed pattern 89 end 90 91 do 92 local s = "hello world from Lua" 93 for w in string.gmatch(s, "%a+") do 94 print(w) 95 end 96 --> =hello 97 --> =world 98 --> =from 99 --> =Lua 100 101 local t = {} 102 local s2 = "from=world, to=Lua" 103 for k, v in string.gmatch(s2, "(%w+)=(%w+)") do 104 t[k] = v 105 end 106 print(t.from, t.to) 107 --> =world Lua 108 109 local errgm = errtest(string.gmatch) 110 111 errgm() 112 --> ~2 arguments needed 113 114 errgm("x") 115 --> ~2 arguments needed 116 117 errgm("x", "%") 118 --> ~malformed pattern 119 120 for w in string.gmatch("abc", "b*") do 121 print(w) 122 end 123 --> = 124 --> =b 125 --> = 126 127 -- Lua 5.4 introduces an optional third argument to string.gmatch, 128 -- specifying where to start the search. 129 130 for w in string.gmatch(s, "%a+", 8) do 131 print(w) 132 end 133 --> =orld 134 --> =from 135 --> =Lua 136 137 for w in string.gmatch(s, "%a+", -1) do 138 print(w) 139 end 140 --> =a 141 142 for w in string.gmatch(s, "%a+", -7) do 143 print(w) 144 end 145 --> =rom 146 --> =Lua 147 148 for w in string.gmatch(s, "%a+", -100) do 149 print(w) 150 end 151 --> =hello 152 --> =world 153 --> =from 154 --> =Lua 155 end 156 157 do 158 local function pgs(...) 159 print(string.gsub(...)) 160 end 161 162 pgs("hello world", "(%w+)", "%1 %1") 163 --> =hello hello world world 2 164 165 pgs("hello world", "%w+", "%0 %0", 1) 166 --> =hello hello world 1 167 168 pgs("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") 169 --> =world hello Lua from 2 170 171 local function getenv(v) 172 if v == "HOME" then 173 return "/home/roberto" 174 elseif v == "USER" then 175 return "roberto" 176 end 177 end 178 179 pgs("home = $HOME, user = $USER", "%$(%w+)", getenv) 180 --> =home = /home/roberto, user = roberto 2 181 182 pgs("4+5 = $return 4+5$", "%$(.-)%$", 183 function (s) 184 return load(s)() 185 end 186 ) 187 --> =4+5 = 9 1 188 189 local t = {name="lua", version="5.3"} 190 pgs("$name-$version.tar.gz", "%$(%w+)", t) 191 --> =lua-5.3.tar.gz 2 192 193 local errgs = errtest(string.gsub) 194 195 errgs() 196 --> ~3 arguments needed 197 198 errgs("x", "y") 199 --> ~3 arguments needed 200 201 errgs("x", "y", "z", "t") 202 --> ~must be an integer 203 204 errgs("x", "%", "z") 205 --> ~malformed pattern 206 207 pgs("xyz", "()y()", "%1-%2") 208 --> =x2-3z 1 209 210 errgs("xyz", "(x)", "%2") 211 --> ~invalid capture index 212 213 pgs("xyz", "xyz", "%%") 214 --> =% 1 215 216 errgs("xyz", "xyz", "%x %%") 217 --> ~invalid.*% 218 219 local replt = {} 220 setmetatable(replt, {__index=function() error("boo") end}) 221 errgs("xyz", "xyz", replt) 222 --> ~boo 223 224 errgs("xyz", "xyz", function () error("baa") end) 225 --> ~baa 226 227 errgs("xyz", "xyz", false) 228 --> ~must be a string, table or function 229 230 errgs("z", "z", {z=true}) 231 --> ~invalid replacement 232 233 pgs("z", "z", {z=false}) 234 --> =z 1 235 236 pgs("abc", "b*", "Z") 237 --> =ZaZcZ 4 238 end