Wutaz
Wutaz

Reputation: 372

Why does string.find return the indexes of the entire pattern, not the first capture?

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?

Upvotes: 3

Views: 324

Answers (1)

lhf
lhf

Reputation: 72422

Captures are a byproduct of matching. Even when you give a patten that has captures, you are still interested in matching the whole pattern. In other words, matching answers the question: where in the given string does this subtext appear? Captures are just extra bits of information about the match.

string.find returns the location of the match to allow you (for instance) to continue parsing the string after the match, possibly with a different pattern.

Upvotes: 2

Related Questions