From: Matt Armstrong Date: 2002-12-14T01:25:21+09:00 Subject: Re: [RCR] Global Regexp Match Mechanism (//g) nobu.nokada@softhome.net writes: >> (b) there is no way to anchor the regex at 'pos' >> >> "abcd".index(/\Abc/, 1) -> nil >> "abcd".index(/^bc/, 1) -> nil > > "abcd".index(/\Gbc/, 1) -> 1 Great! I didn't know you could do that. >> Freezing the string is inconvenient in my application. I examine a >> string in detail before deciding whether to append more data to it >> (String#<<) from a file or start a new string. > > Modification of target string will cause the character boundary > issue even with your String#gpos. It must be recalculated. It is possible to be fancy and avoid recalculation of byte offset B of character position C. E.g. if you insert 10 bytes before B, increment B by 10. But your point about thread safety is good. String#index together with \G is a good solution. Though Regexp#match with a character offset may be handy too, since it returns the MatchData.