[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Finding end of string
- From: "John Logsdon" <j.logsdon@...>
- Date: Fri, 13 Oct 2017 12:30:53 +0100
What is wrong with
str:find("$")-1
or string.find(str,"$")-1
?
> On Tue, Oct 10, 2017 at 10:19 PM, Soni L. wrote:
>
>> After a few hours I came up with this, which works:
>>>
>>> local m, pos
>>> repeat
>>> m, pos = string.match(word_eol[2], "(\\*)"..cw:sub(1,1).."()", pos
>>> or
>>> 2)
>>> until m == nil or #m % 2 == 0
>>>
>>> (cw:sub(1,1) being one of " or ')
>>>
>>>
>
>> Should not the answer rather be 11?
>>>
>>> And would not
>>>
>>> function end_of_string_literal(word,quote)
>>> local pattern = '()%b' .. quote:rep(2) ..'()'
>>> return select(2,word:match(pattern))
>>> end
>>>
>>> be somewhat simpler?
>>>
>>
>> It says "end". It implies you already know the start.
>>
>> "\""
>>
>>
>
> function end_of_string_literal (text, start_pos, quote)
> return text:gsub("\\?.", {[quote]="\0"}):match("%z()", start_pos)
> end
>
> print(end_of_string_literal ([[a="\"A",b="\\\"B\"",c="C"]], 4, '"')) -->
> 8
> print(end_of_string_literal ([[a="\"A",b="\\\"B\"",c="C"]], 12, '"')) -->
> 20
> print(end_of_string_literal ([[a="\"A",b="\\\"B\"",c="C"]], 24, '"')) -->
> 26
>
Best wishes
John
John Logsdon
Quantex Research Ltd
+44 161 445 4951/+44 7717758675