I was interested to learn about the behavior of this code:
str = "abcd"
print( str:find"a(bc)d" ) -- prints 1 4 bc
print( str:find"(ab)cd" ) -- prints 1 4 ab
Although both of these strings look for and return different strings, they return the same indexes because they have the same reference frame. In other words, captures are ignored when calculating indexes, but then they return normally.
My initial question was what went wrong, but then I saw that the manual actually indicates that this is the right behavior (although this is not very clear).
The problem was that I was trying to find something based on the marker next to it, without returning the position of that marker. I expected to string.findreturn the position of the first capture, if there was one, so I just wrapped the part in which I need a position with parentheses. Obviously, this did not help. I have found another (and better) solution to the problem, but I do not think it is always possible or convenient.
Is there a reason to string.findbehave this way? Are there any special benefits for users? If you have the absolute mastery of Lua: is there really no case where this causes a serious problem?
Wutaz source
share