Possible bug: "\b" in regex triggers.

Post Reply
User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Possible bug: "\b" in regex triggers.

Post by tsuujin »

First: here is the code I'm using for my set target alias
[syntax=lua]
hlTarget = tempRegexTrigger("\\b"..target.."\\b",
[[
local c = 1
while selectString(target,c) > -1 do
setFgColor(255,0,0)
setItalics(true)
c = c + 1
end
resetFormat()
]]
)
[/syntax]

Basically, I want the name highlighted in red and made italic. This works, and works fine. The problem is, even with the double escaped "word break" character at the start and end, the word still highlights in the middle of other words!

Anyone else have this problem?

Also, I'd like to know if anyone has gotten the "case insensitive" flag to work properly. (?i) has no effect that I can see, when placed at the start of a pattern.

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

Re: Possible bug: "\b" in regex triggers.

Post by Heiko »

It's not a bug. Your problem is that you select a substring and colorize it - not a regex pattern. You'll want to use selectCaptureGroup(n). Currently, temp triggers don't have a multimatches option. Consequently, you'll only get the first match in a line. However, you can use the return value of selectString() to identify the position of the first character of the selection occurance n and then loop over the current line (stored in the global variable "line") increasing the value of n each time to colorize all matches on this line. To get the same behaviour as \b you need to do further checks on your selection number n. For an example on how to do this checkout my enemy highlighter tech demo in the package section.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Possible bug: "\b" in regex triggers.

Post by tsuujin »

Heiko wrote:It's not a bug. Your problem is that you select a substring and colorize it - not a regex pattern. You'll want to use selectCaptureGroup(n). Currently, temp triggers don't have a multimatches option. Consequently, you'll only get the first match in a line. However, you can use the return value of selectString() to identify the position of the first character of the selection occurance n and then loop over the current line (stored in the global variable "line") increasing the value of n each time to colorize all matches on this line. To get the same behaviour as \b you need to do further checks on your selection number n. For an example on how to do this checkout my enemy highlighter tech demo in the package section.
Yeah, I actually figured out what selectString was doing after I wrote the post, and will have to just go through and do manual checks on each instance of the return.

So, as far as (?i)...??

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Possible bug: "\b" in regex triggers.

Post by tsuujin »

In case anyone else was having the same problem as I was, here is my solution:
[syntax=lua]
target = matches[2]
cecho("\n<white>Target is now: <red><underlined>"..target.."\n")
if hlTarget then killTrigger(hlTarget) end
hlTarget = tempRegexTrigger("\\b"..target.."\\b",
[[
local c = 1
while selectString(target,c) > -1 do
local tLen = target:len()
local pos = selectString(target,c)
local sChar = line:sub(pos,pos)
local eChar = line:sub(pos+tLen+1,pos+tLen+1)
if sChar:match("[^A-Za-z]") and eChar:match("[^A-Za-z]") then
setFgColor(255,0,0)
setItalics(true)
end
c = c + 1
end
resetFormat()
]]
)
resetFormat()
[/syntax]

This colors each match of the target in the line red and italic, but only if it matches the entire word.

Post Reply