Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ MUSHclient
➜ Lua
➜ the lua pcre lib seems to be not usefully complete
the lua pcre lib seems to be not usefully complete
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Fiendish
USA (2,533 posts) Bio
Global Moderator |
Date
| Fri 04 Mar 2016 01:52 AM (UTC) Amended on Fri 04 Mar 2016 02:00 AM (UTC) by Fiendish
|
Message
| There are only 3 exposed functions in MUSHclient's PCRE object (re:exec, re:match, re:gmatch). As far as I can tell, it's impossible to implement something like gsub using them (lrexlib has this). Adding either re:gsub or something that enables the possibility of making a re:gsub would be very useful.
Confirm/Deny? |
https://github.com/fiendish/aardwolfclientpackage | Top |
|
Posted by
| Nick Gammon
Australia (23,120 posts) Bio
Forum Administrator |
Date
| Reply #1 on Fri 04 Mar 2016 02:44 AM (UTC) |
Message
| It doesn't look like that library has been updated since 2004, so you are probably correct. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Fiendish
USA (2,533 posts) Bio
Global Moderator |
Date
| Reply #2 on Fri 04 Mar 2016 03:25 PM (UTC) Amended on Fri 04 Mar 2016 03:26 PM (UTC) by Fiendish
|
Message
| Well, I was wrong. It's not impossible!
Can this be added somehow?
-- like string.gsub but using PCRE
rex.gsub = function(str, re, rep)
if type(re) == "string" then
re = rex.new(re)
end
local output = {}
local startfrom = 1
local s, e, t = re:exec(str, startfrom)
while s ~= nil do
local captures = {}
for i=1,#t,2 do
table.insert(captures, str:sub(t[i], t[i+1]))
end
local filled_rep = rep:gsub("%%(%d+)", function(index) return captures[tonumber(index)] or "" end)
table.insert(output, str:sub(startfrom, s-1))
table.insert(output, filled_rep)
startfrom = e+1
s, e, t = re:exec(str, startfrom)
end
table.insert(output, str:sub(startfrom))
return table.concat(output)
end
|
https://github.com/fiendish/aardwolfclientpackage | Top |
|
Posted by
| Fiendish
USA (2,533 posts) Bio
Global Moderator |
Date
| Reply #3 on Fri 04 Mar 2016 09:42 PM (UTC) Amended on Fri 04 Mar 2016 09:47 PM (UTC) by Fiendish
|
Message
| Actually, my tests seem to show that this is about 25% faster (aardwolf, so luajit 2.1) using
\\\time = utils.timer(); for i=1,100000 do rex.gsub("Nick goes East Man goes West HE GOES HOME", "(\\w+) goes (\\w+)", "%1 de ho") end print(utils.timer()-time)
rex.gsub = function(str, re, rep)
if type(re) == "string" then
re = rex.new(re)
end
output = ""
local startfrom = 1
local s, e, t = re:exec(str, startfrom)
while s ~= nil do
local filled_rep = rep:gsub("%%(%d+)",
function(index)
local i = tonumber(index)*2
return str:sub(t[i-1], t[i]) or ""
end)
output = output..str:sub(startfrom, s-1)..filled_rep
startfrom = e+1
s, e, t = re:exec(str, startfrom)
end
return output..str:sub(startfrom)
end
I guess that's what I get for premature optimization. |
https://github.com/fiendish/aardwolfclientpackage | Top |
|
Posted by
| Nick Gammon
Australia (23,120 posts) Bio
Forum Administrator |
Date
| Reply #4 on Sat 05 Mar 2016 05:27 AM (UTC) |
Message
| "This" is faster than what? You posted two lots of code. (I think). |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Fiendish
USA (2,533 posts) Bio
Global Moderator |
Date
| Reply #5 on Sat 05 Mar 2016 12:01 PM (UTC) |
Message
|
Nick Gammon said:
"This" is faster than what? You posted two lots of code. (I think).
The code in #3 is faster for me than the code in #2 using the simple test in post #3. |
https://github.com/fiendish/aardwolfclientpackage | Top |
|
Posted by
| Fiendish
USA (2,533 posts) Bio
Global Moderator |
Date
| Reply #6 on Sun 21 Jun 2020 09:23 PM (UTC) |
Message
| I think this version also allows using the replacer function form of gsub
rex.gsub = function(str, re, rep)
local output = ""
local as_func = (type(rep) == "function")
local startfrom = 1
local s, e, t = re:match(str, startfrom)
while s ~= nil do
local filled_rep
if as_func then
local substr = str:sub(s,e)
if (#t > 0) then
filled_rep = rep(unpack(t)) or substr
else
filled_rep = rep(substr) or substr
end
else
filled_rep = rep:gsub("%%(%d+)",
function(index)
local i = tonumber(index)*2
return t[i-1] or ""
end)
end
output = output..str:sub(startfrom, s-1)..filled_rep
startfrom = e+1
s, e, t = re:match(str, startfrom)
end
return output..str:sub(startfrom)
end
|
https://github.com/fiendish/aardwolfclientpackage | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
20,604 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top