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

Gammon Software Solutions forum

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

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

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: string.match("You eat orphine.", "You eat (orphine|violet).")
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please).
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Pages: 1 2  

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Thu 01 Jul 2010 04:13 AM (UTC)  quote  ]
Message
Trevize said:
the string is already parsed and the \ is removed.

Yep, that's exactly the problem. =/

Trevize said:
Thanks once again for all the help.

Sure.

'Soludra' on Achaea

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

Posted by Trevize   (21 posts)  [Biography] bio
Date Thu 01 Jul 2010 04:04 AM (UTC)  quote  ]
Message
Thanks alot, it answers all my questions..

Pretty much what I suspected but I had small hopes that there was some way to add like a variable into the long strings, although thinking about it, it would'nt have worked either since the string is already parsed and the \ is removed.. Oh well, I'll deal with it.

Thanks once again for all the help.
[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Thu 01 Jul 2010 03:47 AM (UTC)  quote  ]

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

Message
Trevize said:

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!



Use this.

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


Or this.

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



It's really the only way. This is something you have to deal with at the syntactic level. Once the string is parsed, there's no going back to fiddle with the escapes.

'Soludra' on Achaea

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

Posted by Trevize   (21 posts)  [Biography] bio
Date Thu 01 Jul 2010 03:42 AM (UTC)  quote  ]
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

Posted by Nick Gammon   Australia  (18,800 posts)  [Biography] bio   Forum Administrator
Date Thu 01 Jul 2010 12:01 AM (UTC)  quote  ]
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 Twisol   USA  (2,230 posts)  [Biography] bio
Date Wed 30 Jun 2010 10:37 PM (UTC)  quote  ]

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 Trevize   (21 posts)  [Biography] bio
Date Wed 30 Jun 2010 10:09 PM (UTC)  quote  ]
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 Nick Gammon   Australia  (18,800 posts)  [Biography] bio   Forum Administrator
Date Wed 30 Jun 2010 09:33 PM (UTC)  quote  ]
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 Wed 30 Jun 2010 01:41 PM (UTC)  quote  ]

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 Larkin   (278 posts)  [Biography] bio
Date Mon 28 Jun 2010 11:35 PM (UTC)  quote  ]
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 Nick Gammon   Australia  (18,800 posts)  [Biography] bio   Forum Administrator
Date Sun 27 Jun 2010 10:01 PM (UTC)  quote  ]

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 Twisol   USA  (2,230 posts)  [Biography] bio
Date Sun 27 Jun 2010 12:08 AM (UTC)  quote  ]

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 Trevize   (21 posts)  [Biography] bio
Date Sun 27 Jun 2010 12:01 AM (UTC)  quote  ]
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 Trevize   (21 posts)  [Biography] bio
Date Thu 24 Jun 2010 12:00 PM (UTC)  quote  ]
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 Twisol   USA  (2,230 posts)  [Biography] bio
Date Wed 23 Jun 2010 04:32 AM (UTC)  quote  ]
Message
Excellent point! I missed that.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[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.


5,036 views.

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

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

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

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]