3

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?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Wutaz
  • 362
  • 3
  • 19
  • 2
    Use empty captions to get the position: `print(str:match"a()(bc)()d") -- 2 bc 4` – Egor Skriptunoff Feb 16 '14 at 18:30
  • When you do that with `find` it does return the positions of the empty captures, but they are returned as captures, so it doesn't change or explain the strange behavior. – Wutaz Feb 16 '14 at 18:40
  • 2
    Use `match` to get captures. Use `find` to get indices and captures. Simply don't use `find` if it does not reflect your needs. – Egor Skriptunoff Feb 16 '14 at 20:25

1 Answers1

2

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.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • 1
    You seem to be re-creating the question as a statement. I still don't see why anyone would want the function to behave like this. Telling me that I want something doesn't mean that I do. – Wutaz Feb 24 '14 at 23:37