Need help pattern matching

Darmir
Posts: 226
Joined: Sun May 01, 2011 6:51 pm
Contact:

Re: Need help pattern matching

Post by Darmir »

Heiko,
I don't get it? I do select each match.. Here is the code.
Code: [show] | [select all] lua
local a,b,c
pattern: ^\s+([\w-]+)(?:\s+([\w-]+))?(?:\s+([\w-]+))?\s+$

a = matches[2]
b = matches[3]
c = matches[4]
selectString(a, 1)
setLink([[send("craft ]]..a..[[")]], "Can I do any "..a.." crafts?")
setUnderline(true)
setFgColor(218,165,32)
setBgColor(0,0,0)

selectString(b, 1)
setLink([[send("craft ]]..b..[[")]], "Can I do any "..b.." crafts?")
setUnderline(true)
--setFgColor(218,165,32)
--setBgColor(0,0,0)
--resetFormat()

selectString(c, 1)
setLink([[send("craft ]]..c..[[")]], "Can I do any "..c.." crafts?")
setUnderline(true)
--setFgColor(218,165,32)
--setBgColor(0,0,0)
resetFormat()
What is missing?

Yetzederixx
Posts: 186
Joined: Sun Nov 14, 2010 5:57 am

Re: Need help pattern matching

Post by Yetzederixx »

Not sure why it's not highlighting everything honestly, I tested the regex again at this site http://www.regextester.com/ and it worked fine.
Code: [show] | [select all] lua
local i = 2

--this could be done with a generic for loop, but I'm tired
--I'm guessing that you want it to send a command when you click one of those words and that
--  is what this code will/should do.
while i <= #matches do
    moveCursor(selectString(matches[i], 1), getLineNumber())
    replace("")
    insertLink(matches[2], [[send("YOUR_COMMAND_TO_DO_ON ]]..matches[i]..[[")]], matches[i])
    i = i + 1
end

Darmir
Posts: 226
Joined: Sun May 01, 2011 6:51 pm
Contact:

Re: Need help pattern matching

Post by Darmir »

Yeah, I think there is a bug in the code that is causing that issue. I just put in the loop you provided and two words are not being changed. The loop you provided doesn't look as good as the way I did it.

Going off of your idea with a loop I changed it a little to get it to format better on the output.
Here is the code:
Code: [show] | [select all] lua
local i = 2

--this could be done with a generic for loop, but I'm tired
--I'm guessing that you want it to send a command when you click one of those words and that
--  is what this code will/should do.
while i <= #matches do
	moveCursor(selectString(matches[i], 1), getLineNumber())
   	setLink([[send("craft ]]..matches[i]..[[")]], "Can I do any "..matches[i].." crafts?")    
	setUnderline(true)
	setFgColor(218,165,32)
	setBgColor(0,0,0)
    i = i + 1
end
resetFormat()

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Need help pattern matching

Post by Heiko »

It's certainly not a bug, but you haven't looked at the examples in the manual :) It's all there.
You need to change your code to selectString( what, whichMatchOnTheLine ).
I try another example:
Let's say we get this line from the MUD:
"Jim saw Jim's friend Tom who happens to be Jim's cousin."
If you want to highlight all matches of the substring "Jim" on this line, you need to do this:
selectString( "Jim", 1 )
setFgColor(255,0,0)
selectString("Jim", 2 )
setFgColor(255,0,0)
selectString("Jim",3)
setFgColor(255,0,0)

selectString() is a low level substring selection function that operates on the line where the current user cursor (->moveCursor()) is positioned.
As I've pointed out before you can use the return value of selectString() to loop over the line or use the more high level function selectCaptureGroup().
-> manual

Darmir
Posts: 226
Joined: Sun May 01, 2011 6:51 pm
Contact:

Re: Need help pattern matching

Post by Darmir »

Heiko,
I figured it out. Thanks for the information.

Post Reply