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.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ PCRE Regex help
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Madcowchow
Kenya (6 posts) Bio
|
Date
| Mon 06 Nov 2017 02:00 PM (UTC) Amended on Mon 06 Nov 2017 02:26 PM (UTC) by Madcowchow
|
Message
| Hello Friends
Please point me to the right direction how I can extract the table with a regex as below.
local mystring = "You are madcowchow (dark-elf, archer) aged 195 years."
local re = rex.new("^You are (?<name>\w+) \((?<tribe>\S+), (?<job>\w+)\) aged (?<age>\d+) years\.$")
local s, e, t = re:match(mystring)
print (type(t))
edit: i am expecting output to return 'table', but get 'nil'.
the regex extracts the fields when i test it at https://regex101.com/
Thank you | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #1 on Mon 06 Nov 2017 08:51 PM (UTC) Amended on Mon 06 Nov 2017 09:33 PM (UTC) by Nick Gammon
|
Message
| Your literal strings have backslashes in them, which Lua processes before it even gets to the regexp. Thus you have to double them, like below:
local mystring = "You are madcowchow (dark-elf, archer) aged 195 years."
local re = rex.new("^You are (?<name>\\w+) \\((?<tribe>\\S+), (?<job>\\w+)\\) aged (?<age>\\d+) years\\.$")
local s, e, t = re:match(mystring)
print (type(t))
require "tprint"
tprint (t)
Result:
table
1="madcowchow"
2="dark-elf"
3="archer"
4="195"
"tribe"="dark-elf"
"name"="madcowchow"
"age"="195"
"job"="archer"
This is probably why Lua uses "%" in its pattern matching rather than "\" to save the trouble of doing that. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Madcowchow
Kenya (6 posts) Bio
|
Date
| Reply #2 on Mon 06 Nov 2017 09:34 PM (UTC) |
Message
| Thank you Nick, much appreciated. Spent most of my morning trying to see where I failed. I guess you tend to forget stuff as u grow older :) | Top |
|
Posted by
| Marco
(36 posts) Bio
|
Date
| Reply #3 on Tue 07 Nov 2017 11:49 AM (UTC) |
Message
| Sorry if i ask, but i'm very interested in this topic too.
Looking at the code and understanding what you've said (Nick) :
local mystring = "You are madcowchow (dark-elf, archer) aged 195 years."
local re = rex.new("^You are (?<name>\\w+) \\((?<tribe>\\S+), (?<job>\\w+)\\) aged (?<age>\\d+) years\\.$")
local s, e, t = re:match(mystring)
print (type(t))
require "tprint"
tprint (t)
Result:
table
1="madcowchow"
2="dark-elf"
3="archer"
4="195"
"tribe"="dark-elf"
"name"="madcowchow"
"age"="195"
"job"="archer"
Is it normal that if i put something similar in a Trigger (or even an alias) and in the SendTo Script it doesn't work?
It is supposed to work correctly only in a Script file ? | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #4 on Tue 07 Nov 2017 08:06 PM (UTC) |
Message
| The problem with triggers and aliases (using the Send box) is that the client expands out backslashes first, then sends it to Lua which expands backslashes again. Thus in Send To Script you have to double the backslashes again (that is, four backslashes for every one you really want).
This is obviously fairly tedious. I suggest using a script file, or using a Lua pattern (eg. string.match) which avoids the problem because it uses "%" to escape special fields (like alphabetic sequences) instead of "\". |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | 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.
18,029 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top