[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  PCRE Regex help
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

PCRE Regex help

You need to log onto the forum to reply or create new threads.

  [Refresh] Refresh page


Posted by Madcowchow   Kenya  (6 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] top

Posted by Madcowchow   Kenya  (6 posts)  [Biography] 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 :)
[Go to top] top

Posted by Marco   (36 posts)  [Biography] 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?
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] 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.


773 views.

You need to log onto the forum to reply or create new threads.

  [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]