a Test with 'rex_pcre' lib..

Posted by Maxhrk on Mon 24 Dec 2007 10:37 PM — 6 posts, 26,583 views.

USA #0
This experiment that i want to find out how good Rex_pcre lib is... since it probably very new library. Nevertheless I found something interesting while playing with this kind of experiment.

Also this experiment also including by go against mushclient's trigger's regex pattern matching.


the regex line would be this:


"\((?P<channel>.+)\): (?P<who>.+?)(| \((?P<where>.+)\)) say(|s), \"(?P<comment>.+)\"


and trigger line is thus:

(Serenwilde): You say, "Hmmhmm."


the
this is lua code i use to check the wildcards. as usual everybody no doubt everybody use it anyway!


for key, value in pairs(wildcards) do
		if type(value) == "boolean" then
			print("KEY:" .. key .. "  value:" .. tostring(value))
		else
			print("KEY:" .. key .. "  value:" .. value)
		end --if
	end --if



mushclient's wildcards match:

(Serenwilde): You say, "Hmmhmm."
KEY:1  value:Serenwilde
KEY:2  value:You
KEY:3  value:
KEY:4  value:
KEY:5  value:
KEY:6  value:Hmmhmm.
KEY:comment  value:Hmmhmm.
KEY:channel  value:Serenwilde
KEY:0  value:(Serenwilde): You say, "Hmmhmm."
KEY:who  value:You
KEY:where  value:


rex_pcre lib's wildcards match:

id:1  v:(Serenwilde)
id:2  v:(Serenwilde)
id:3  v:You
id:4  v:
id:5  v:false
id:6  v:false
id:7  v:s
id:8  v:Hmmhmm.
id:who  v:You
id:where  v:false
id:comment  v:Hmmhmm.
id:channel  v:(Serenwilde)


notice the difference? it's the channel wildcard. In fact I was expect it to be just 'Serenwilde' not '(Serenwilde)'


The the version of rex_pcre library i am using is 2.2. Just a thought!
Amended on Tue 25 Dec 2007 04:08 PM by Maxhrk
Australia Forum Administrator #1
Can you clarify which rex_pcre library you are referring to? There is an inbuilt PCRE matcher in MUSHclient (virtually unchanged from the one from the PCRE web site), which is used in both trigger and alias matching, as well as the inbuilt "rex" library, see this link:

http://www.gammon.com.au/scripts/doc.php?general=lua_rex

There is also the Lua regular expression matching which is something different again.

Now you mention rex_pcre, is that a DLL you got from a Lua site? If so it is possibly a different version of PCRE to the one I am using. The presence of the parentheses is puzzling however.
USA #2
oh mushclient has builtin pcre function? i better try this out then!
USA #3
here is mushclient's builtin rex test:


key:1  value:(Serenwilde)
key:2  value:(Serenwilde)
key:3  value:You
key:4  value:
key:5  value:false
key:6  value:false
key:7  value:
key:8  value:Hmmhmm.
key:who  value:You
key:where  value:false
key:comment  value:Hmmhmm.
key:channel  value:(Serenwilde)


AMEND:

well well that is interesting. i decide to add extra '\' beside other backslash like this:


"\\((?P<channel>.+)\\): (?P<who>.+?)(| \\((?P<where>.+)\\)) say(|s), \"(?P<comment>.+)\""


key:1 value:Serenwilde
key:2 value:You
key:3 value:
key:4 value:false
key:5 value:false
key:6 value:
key:7 value:Hmmhmm.
key:who value:You
key:where value:false
key:comment value:Hmmhmm.
key:channel value:Serenwilde

AMEND(last time):
I tried to do same with rex_pcre, it seem got a right value. I guess i have to put double backslash next time to have a correct value. :p


P.S. Merry Christmas. :)
Amended on Tue 25 Dec 2007 06:32 PM by Maxhrk
Australia Forum Administrator #4
You need to be cautious with backslashes in the "send" field of triggers and aliases in MUSHclient.

MUSHclient automatically handles things like \n in the Send field and translates them into a linefeed. Thus, to actually send \ you need to put \\ there.

However if using Lua scripting, Lua also turns \\ into \, so to do something like a Note that notes a single \, and using "send to script" you actually have to do this:

Note ("\\\\")

MUSHclient converts \\\\ to \\ before handing it off to Lua, and Lua converts \\ to \, which is what gets printed.

This makes doing PCRE regular expressions a bit fiddly. One way is to use the script file (rather than "send to script") which halves the number of backslashes you need.

Then inside the script file, if you need to make a regular expression you could use multi-line comments, which then removes the need to escape backslashes at all. For example:

myregexp = [[ ..... put it here ...... ]]
Australia Forum Administrator #5
Incidentally, Lua regular expressions use % as a special character (eg. %a for alphabetic characters) thus getting around the need to double the number of backslashes in quoted strings.