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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  string.match("You eat orphine.", "You eat (orphine|violet).")

string.match("You eat orphine.", "You eat (orphine|violet).")

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Pages: 1 2  

Posted by Trevize   (21 posts)  [Biography] bio
Date Tue 22 Jun 2010 03:27 PM (UTC)
Message
First off, sorry for a ridiculous post but I'm having a headache and cant continue before I fix this..

string.match("You eat orphine.", "You eat (orphine|violet).")

Like the you can see above, is there possible to match that like I can do with MUSHclients own triggers? I already know that I could just switch (orphine|violet) to (%w+) or a number of things. But I -really- want to match those two specific words and -only-..

It just makes the system a little bit more illusion safe and the functions that gets the captures later dont need extra checks.

Thanks for any help..
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #1 on Tue 22 Jun 2010 06:31 PM (UTC)
Message
Option one:

local words = {["orphine"] = true, ["violet"] = true}

local foo = string.match("You eat orphine.", "You eat (%w+).")
if words[foo] then
  -- we have a match
end



Option two:

re = rex.new("^You eat (orphine|violet)\.$")
local start, end = re:match("You eat orphine.")
if start then
  -- we have a match
end


With option two, it's a good idea to compile the regexp only once (with rex.new), and reuse it each time you need it. Also, option two uses the same regexp library as MUSHclient (PCRE).

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Wed 23 Jun 2010 04:17 AM (UTC)
Message
Twisol said:

Option one:

local words = {["orphine"] = true, ["violet"] = true}

local foo = string.match("You eat orphine.", "You eat (%w+).")
if words[foo] then
  -- we have a match
end




This will fail with indexing a nil value if there is no match, so it should probably read:



local foo = string.match("You eat orphine.", "You eat (%w+).")
if foo and words[foo] then
  -- we have a match
end


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #3 on Wed 23 Jun 2010 04:32 AM (UTC)
Message
Excellent point! I missed that.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Trevize   (21 posts)  [Biography] bio
Date Reply #4 on Thu 24 Jun 2010 12:00 PM (UTC)
Message
Perfect, I've got PCRE working with the proxy I'm using now so everything is working properly with that kind of match function.

My thanks
[Go to top] top

Posted by Trevize   (21 posts)  [Biography] bio
Date Reply #5 on Sun 27 Jun 2010 12:01 AM (UTC)
Message
Can anyone explain why this does -not- work

echo(C.Y .. tostring(rex.match("You 608 Points.", "^You (\d+) Points\.$")))

if rex.match("You 608 Points.", "^You (\d+) Points\.$") then
echo(C.G .. "omfg")
end

And this does:

echo(C.Y .. tostring(rex.match("You 608 Points.", "^You (\\d+) Points\.$")))

if rex.match("You 608 Points.", "^You (\\d+) Points\.$") then
echo(C.G .. "omfg")
end

Can it have something to do with the fact that I'm coding in LUA and it uses "\" as an escape in strings or something? :S
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #6 on Sun 27 Jun 2010 12:08 AM (UTC)

Amended on Sun 27 Jun 2010 12:09 AM (UTC) by Twisol

Message
Yes, that's correct. You can get around that using a long-string, like [[this]].

echo(C.Y .. tostring(rex.match("You 608 Points.", [[^You (\d+) Points\.$]])))

if rex.match("You 608 Points.", [[^You (\d+) Points\.$]]) then
  echo(C.G .. "omfg")
end


Long strings are very nice for things like regular expressions.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Sun 27 Jun 2010 10:01 PM (UTC)

Amended on Sun 27 Jun 2010 10:02 PM (UTC) by Nick Gammon

Message
This annoyance is probably why Lua uses % in its regexps and not \.

I should also point out that if you are using "send to script" then MUSHclient already turns \\ into \ before even handing over to Lua, so in "send to script" you need to use \\\\ inside a Lua string, to get a single \ inside that string.

However if using a script file, you don't need to worry about that (but you would still need to use \\ to put a single \ inside a Lua string).

Another "gotcha" in "send to script" is that MUSHclient uses % to recognize wildcards (eg. %1 is the first wildcard) and Lua also uses %1 for things like string.gsub where %1 refers to the first "found group".

So in "send to script" you need to double % to %% for it to work properly.

For both of these reasons, scripting in a script file (or the script part of a plugin) can be less of a hassle.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Larkin   (278 posts)  [Biography] bio
Date Reply #8 on Mon 28 Jun 2010 11:35 PM (UTC)
Message
I think he's coding this in MudBot's ILua module, not MUSHclient itself. I recognize the color codes table.
[Go to top] top

Posted by Trevize   (21 posts)  [Biography] bio
Date Reply #9 on Wed 30 Jun 2010 01:41 PM (UTC)

Amended on Wed 30 Jun 2010 01:50 PM (UTC) by Trevize

Message
Yes, I'm coding in Mudbot although I'm still using MUSH as my client..

And I've run into another problem that I cant work out...

It seems to me that it's impossible to add any sort of variable value into long strings.

I'm working on my own trigger parser, it's basic form looks something like this.


for k, v in pairs(tLines) do
for _, t in pairs(tTriggers) do
if rex.match(v, [[t.pattern]]) then
echo("Matched")
end
end
end

Think that explains what I want to do, but the problem is that anything inbetween [[ and ]] except [] are handled as string?

Edit: I'm guessing I could just add a longstring into the table, but then I run into another problem with the function I'm using to add triggers.. since it's basicly called: triggerAdd(pattern, otherstuff1, otherstuff2)
Like: triggerAdd([[^You (\d+) Points\.$]], 'echo("matched")', nil)
That would probably work, but I'd like to not have to use the [[ ]] in the triggerAdd call.. And rather in the function, and when I've tried that it adds it like this: [['^You (\d+) Points\.$']]
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Wed 30 Jun 2010 09:33 PM (UTC)
Message
Trevize said:

It seems to me that it's impossible to add any sort of variable value into long strings.


Well for variables, you just don't quote them.

I don't see why you are putting [[ ... ]] around what is just a variable.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Trevize   (21 posts)  [Biography] bio
Date Reply #11 on Wed 30 Jun 2010 10:09 PM (UTC)
Message
Because otherwise I'm back to the same problem I had before where I had to escape the "\" in my LUA code..

I guess I should be happy that it's just working, but I'd really want to make this basic/core functions in my new system perfect..

Like I sayd, making a trigger parser right now, and using PCRE now so that I can use matches like (orphine|violet) and get the captures to use. And I'm having a function to add these triggers into the table to check with the output from the mud.

So I need to either figure out how to use [[ ]] (longstrings) with an variable in it from the trigger table. Or another way of making captures like PCRE can do, or third. Just make every trigger add the pattern as a longstring to begin with, like table.insert(tTriggers, { pattern = [[^You (\d+) Points\.$]] }). OR escape each "\"..
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #12 on Wed 30 Jun 2010 10:37 PM (UTC)

Amended on Thu 01 Jul 2010 12:01 AM (UTC) by Twisol

Message
The escaping thing only matters for strings that are actually visible in the code. String quoting - "", '', [[]] - are just ways for you, the programmer to tell the program what a string is. Once the string is assigned, the program already knows what the string looks like, internally. You'll notice that strings never have the "", '', [[]] included in the actual value unless you add them yourself, either. It's just a syntactic way to get your intent across to the program.

(bolded for emphasis, not anger)

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #13 on Thu 01 Jul 2010 12:01 AM (UTC)
Message
Trevize said:


So I need to either figure out how to use [[ ]] (longstrings) with an variable in it from the trigger table.


You are confusing two operations here.

Long strings are only needed when you literally supply a string, eg.


name = "Nick Gammon"


Now if the thing in quotes might have backslashes in it, you can use long strings:


name = [[Nick \ Gammon]]



But once you have a variable (eg. name in this case) long strings are irrelevant. A variable can contain backslashes, newlines, etc.

So, whatever "name" has in it, you can do this:


if rex.match(v, name) then
  echo("Matched")
end



- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Trevize   (21 posts)  [Biography] bio
Date Reply #14 on Thu 01 Jul 2010 03:42 AM (UTC)
Message
Am I a retard again or not making my point clear :(

trigger_add("^You (\d+) Points\.$", nil, nil, "Replaced")

function trigger_add(pattern, send, script, replace)
table.insert(tTriggers, { pattern = pattern, send = send, script = script, replace = replace })
end

This is how I add my "so called" triggers into the table that checks against the mud's output. The problem I'm having is that the string that's in tTriggers table now does'nt match because it's like this: ^You (d+) Points.$

You showed me that I could use [[ ]] to fix that problem, but it seems to me that the only way to add a long string like that with my function is to supply it in the "call" to the function, like:
trigger_add([[^You (\d+) Points\.$]], nil, nil, "Replaced")

And I was hoping that I would'nt have to do that because (to me) it looks more ugly and possible harder for anyone else using this function to understand. I had hopes that I could do something like I have, and just make the insert do this: table.insert(tTriggers, { pattern = [[pattern]], send = send, script = script, replace = replace }) or something similiar..

Anyway I'm starting to think that it's either that or double "\" to escape the second backslash..

Thanks for your time and patience with me and this thread. It's much appreciated!
[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.


45,783 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     [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 HostDash]