第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

我如何在 Lua 中使用參數(shù)實(shí)現(xiàn) rfind ?

我如何在 Lua 中使用參數(shù)實(shí)現(xiàn) rfind ?

莫回?zé)o 2023-09-05 21:07:15
例如,我想在lua中做這樣的事情:s = "Hey\n There And Yea\n"print(s.rfind("\n", 0, 5))我嘗試在 lua 中使用 string.find 函數(shù)進(jìn)行此操作:local s = "Hey\n There And Yea\n"local _, p = s:find(".*\n", -5)print(p)但這些并沒(méi)有產(chǎn)生相同的結(jié)果。我做錯(cuò)了什么,如何解決這個(gè)問(wèn)題,使其與 rfind 相同?
查看完整描述

2 回答

?
智慧大石

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊

Lua 有一個(gè)鮮為人知的函數(shù)string.reverse,可以反轉(zhuǎn)字符串中的所有字符。雖然很少需要這樣做,但該函數(shù)通??捎糜谠谧址畠?nèi)進(jìn)行反向搜索。因此,要實(shí)現(xiàn)rfind,您需要在反向原始字符串中搜索反向模式,最后進(jìn)行一些算術(shù)以獲得與原始字符串的偏移量。這是模仿 Python 的代碼:rfind


function rfind(subject, tofind, startIdx, endIdx)

    startIdx = startIdx or 0

    endIdx = endIdx or #subject

    subject = subject:sub(startIdx+1, endIdx):reverse()

    tofind = tofind:reverse()

    local idx = subject:find(tofind)

    return idx and #subject - #tofind - idx + startIdx + 1 or -1

end


print(rfind("Hello World", "H")) --> 0

print(rfind("Hello World", "l")) --> 9

print(rfind("foo foo foo", "foo")) --> 8

print(rfind("Hello World", "Toto")) --> -1

print(rfind("Hello World", "l", 1, 4)) --> 3

請(qǐng)注意,此版本rfind使用 Python 索引約定,從 開(kāi)始,如果未找到字符串則0返回。在 Lua 中使用基于 1 的索引并在沒(méi)有匹配時(shí)-1返回會(huì)更加連貫。nil修改將是微不足道的。


查看完整回答
反對(duì) 回復(fù) 2023-09-05
?
繁華開(kāi)滿(mǎn)天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊

我編寫(xiě)的模式僅適用于單字符子字符串,例如提問(wèn)者用作測(cè)試用例的子字符串。跳到下一個(gè)粗體標(biāo)題以查看答案,或者繼續(xù)閱讀以了解他們?cè)趪L試中做錯(cuò)的一些事情的解釋。跳到最后的粗體標(biāo)題,以獲得多字符子字符串的通用、低效解決方案


我嘗試mystring.rfind使用 lua重新創(chuàng)建 python 的輸出mystring:find,它僅適用于單字符子字符串。稍后我將向您展示一個(gè)適用于所有情況的函數(shù),但這是一個(gè)非常糟糕的循環(huán)。


作為回顧(為了解決你做錯(cuò)的事情),讓我們來(lái)談?wù)刴ystringvar:find("pattern", index), 的糖string.find(mystringvar, "pattern", index)。這將返回start, stop索引。


可選的 Index 設(shè)置開(kāi)始,而不是結(jié)束,但負(fù)索引將從“右減索引”向后計(jì)數(shù)到字符串末尾(索引 -1 將僅計(jì)算最后一個(gè)字符,-2 將計(jì)算最后 2 個(gè)字符)。這不是期望的行為。


您不應(yīng)嘗試使用索引來(lái)創(chuàng)建子字符串,而應(yīng)該創(chuàng)建如下所示的子字符串:

mystringvar:sub(start, end)將從頭到尾提取并返回子字符串(1 個(gè)索引,包括結(jié)尾)。因此,要重新創(chuàng)建 Python 的 0-5(0 索引,獨(dú)占結(jié)尾),請(qǐng)使用 1-5。


現(xiàn)在請(qǐng)注意,這些方法可以鏈接在一起string:sub(x, y):find(""),但為了便于閱讀,我將其分解。話(huà)不多說(shuō),我向您介紹:

答案


local s = "Hey\n There And Yea\n"

local substr = s:sub(1,5)

local start, fin = substr:find("\n[^\n]-$")

print(start, ",", fin)

我有一些半措施解決方案,但為了確保我所編寫(xiě)的內(nèi)容適用于多個(gè)子字符串實(shí)例(1-5 子字符串僅包含 1),我使用子字符串和整個(gè)字符串進(jìn)行了測(cè)試。觀察:

用 sub(1, 5) 輸出: 4   ,  5

用 sub(1, 19) 輸出(整個(gè)長(zhǎng)度):19   ,  19


它們都正確地報(bào)告了最右邊子字符串的開(kāi)頭,但請(qǐng)注意,“fin”索引位于句子的末尾,我將在稍后解釋。我希望這沒(méi)問(wèn)題,因?yàn)?rfind 無(wú)論如何都只返回起始索引,所以這應(yīng)該是一個(gè)合適的替代品。


讓我們重讀一下代碼,看看它是如何工作的:

sub 我已經(jīng)解釋過(guò)了

string.find 中不再需要索引 好吧

,這個(gè)模式是什么"\n[^\n]-$"?

$- 錨定到句子末尾

[^x]- 匹配“not x”

-- 前一個(gè)字符或集合(在本例中為 )的匹配盡可能少(甚至 0)[^\n]。這意味著如果一個(gè)字符串以您的子字符串結(jié)尾,它仍然可以工作)

它以 \n 開(kāi)頭,所以總的來(lái)說(shuō)它的意思是:“給我一個(gè)換行符,但后面沒(méi)有其他換行符,直到句子結(jié)尾”。這意味著即使您的子字符串僅包含 1 個(gè) \n 實(shí)例,如果您要在具有多個(gè)子字符串的字符串上使用此函數(shù),您仍然會(huì)獲得最高索引,就像 rfind 一樣。


請(qǐng)注意, string.find 不符合模式組 ( ()),因此將其包裝\n在組中是徒勞的。因此,我無(wú)法阻止末端錨定$將變量擴(kuò)展fin到句子的末尾。


我希望這對(duì)你有用。


對(duì)任意長(zhǎng)度的子字符串執(zhí)行此操作的函數(shù)

我不會(huì)解釋這一點(diǎn)。


function string.rfind(str, substr, plain) --plain is included for you to pass to find if you wish to ignore patterns

  assert(substr ~= "") --An empty substring would cause an endless loop. Bad!

  local plain = plain or false --default plain to false if not included

  local index = 0

  --[[

    Watch closely... we continually shift the starting point after each found index until nothing is left. 

        At that point, we find the difference between the original string's length and the new string's length, to see how many characters we cut out. 

  ]]--

  while true do

    local new_start, _ = string.find(str, substr, index, plain) --index will continually push up the string to after whenever the last index was.

    if new_start == nil then --no match is found

            if index == 0 then return nil end   --if no match is found and the index was never changed, return nil (there was no match)

      return #str - #str:sub(index)  --if no match is found and we have some index, do math.

    end

    --print("new start", new_start)

    index = new_start + 1 --ok, there was some kind of match. set our index to whatever that was, and add 1 so that we don't get stuck in a loop of rematching the start of our substring.

  end

end

如果您想查看我的整個(gè)“測(cè)試套件” ......


查看完整回答
反對(duì) 回復(fù) 2023-09-05
  • 2 回答
  • 0 關(guān)注
  • 232 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)