I was wondering 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
Even though both of the two lines are looking for, and return, different strings, they return the same indices because they have the same frame of reference. In other words, the captures are ignored when calculating the indices, but then they are returned normally.
My original question was going to be about what went wrong, but then I saw that the manual actually indicates that this is proper behavior (though it isn't very clear).
The problem was that I was trying to find something based on a marker near it, without returning the position of that marker. I expected string.find
to return the position of the first capture, if there was one, so I just wrapped the part I wanted the position of with parenthesis. Obviously, that didn't help. I found a different (and better) solution to the problem, but I don't think that is always possible or convenient.
Is there any reason for string.find
to behave this way? Is there any particular benefit for users? If you have absolute mastery of Lua: is there actually no case where this causes a serious problem?