如何在lua字符串中找到重复字符的索引

发布于 2025-01-23 07:39:26 字数 154 浏览 0 评论 0原文

假设你有这样的路 /home/user/dev/project 我想获得任何/的索引,我想要
就像我想要dev之前的一个或用户
之前的一个 如果有一个好的文档,请链接它

suppose you have a path like this
/home/user/dev/project
I want to get the index of any / I want

like if I want the one before dev or the one before user

I don't get lua string patterns if there is a good documentation for it please link it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国产ˉ祖宗 2025-01-30 07:39:26

有几种方法可以做到这一点。也许最简单的是使用()模式元素,该模式元素产生的匹配位置与string.gmatch

for index in ("/home/user/dev/project"):gmatch"()/" do
    print(index)
end

1
6
11
15

按预期进行打印。要走的另一种方法(需要更多代码)将反复调用string.find,始终通过启动索引。

假设您可能想将字符串划分为斜线,则使用string.gmatch :(

for substr in ("/home/user/dev/project"):gmatch"[^/]+" do
    print(substr)
end

字符串,最大长度不包含斜线的所有子字符)

该模式找到所有非零的子 IS 在这里。您可能想查看“捕获”小节。

There are several ways to do this. Perhaps the simplest is using the () pattern element which yields a match position combined with string.gmatch:

for index in ("/home/user/dev/project"):gmatch"()/" do
    print(index)
end

which prints

1
6
11
15

as expected. Another way to go (which requires some more code) would be repeatedly invoking string.find, always passing a start index.

Assuming that you probably want to split a string by slashes, that's about as simple using string.gmatch:

for substr in ("/home/user/dev/project"):gmatch"[^/]+" do
    print(substr)
end

(the pattern finds all substrings of nonzero, maximal length that don't contain a slash)

Documentation for patterns is here. You might want to have a look at the subsection "Captures".

古镇旧梦 2025-01-30 07:39:26

有很多方法。
也很高兴知道LUA已将所有字符串函数连接为方法。
那就是@lmd用直接在字符串上演示的内容。
我最喜欢尝试使用模式及其捕获的复杂/困难的地方的地方是Lua独立控制台:制作Linux-Readline

因此,让我们玩模式'[%/\\] [%u%l%s]+'

> _VERSION
Lua 5.4
> -- Lets set up a path
> path='/home/dev/project/folder with spaces mixed with one OR MORE Capitals in should not be ignored'
> -- I am curious /home exists so trying to have a look into
> os.execute('/bin/ls -Ah ' .. ('"%s"'):format(path:match('[%/\\][%u%l%s]+')));
knoppix  koyaanisqatsi
> -- OK now lets see if i can capture the last folder with the $
> io.stdout:write(('"%s"\n'):format(path:match('[%/\\][%u%l%s]+
))):flush();
"/folder with spaces mixed with one OR MORE Capitals in should not be ignored"
> -- Works too so now i want to know whats the depth is
> do local str, count = path:gsub('[%/\\][%u%l%s%_%-]+','"%1"\n') print(str) return count end
"/home"
"/dev"
"/project"
"/folder with spaces mixed with one OR MORE Capitals in should not be ignored"

4
> -- OK seems usefull lets check a windows path with it
> path='C:\\tmp\\Some Folder'
> do local str, count = path:gsub('[%/\\][%u%l%s]+','<%1>') print(str) return count end
C:<\tmp><\Some Folder>
2
> -- And that is what i mean with "many"
> -- But aware that only lower upper and space chars are handled
> -- So _ - and other chars has to be included by the pattern
> -- Like: '[%/\\][%u%l%s%_%-]+'
> path='C:\\tmp\\Some_Folder'
> do local str, count = path:gsub('[%/\\][%u%l%s%_%-]+','<%1>') print(str) return count end
C:<\tmp><\Some_Folder>
2
> path='C:\\tmp\\Some-Folder'
> do local str, count = path:gsub('[%/\\][%u%l%s%_%-]+','<%1>') print(str) return count end
C:<\tmp><\Some-Folder>
2

There are many ways to do so.
Also its good to know that Lua has attached all string functions on datatype string as methods.
Thats what @LMD demonstrates with the : directly on a string.
My favorite place for experimenting with such complicated/difficult things like pattern and their captures is the Lua Standalone Console maked with: make linux-readline
So lets play with the pattern '[%/\\][%u%l%s]+'

> _VERSION
Lua 5.4
> -- Lets set up a path
> path='/home/dev/project/folder with spaces mixed with one OR MORE Capitals in should not be ignored'
> -- I am curious /home exists so trying to have a look into
> os.execute('/bin/ls -Ah ' .. ('"%s"'):format(path:match('[%/\\][%u%l%s]+')));
knoppix  koyaanisqatsi
> -- OK now lets see if i can capture the last folder with the $
> io.stdout:write(('"%s"\n'):format(path:match('[%/\\][%u%l%s]+
))):flush();
"/folder with spaces mixed with one OR MORE Capitals in should not be ignored"
> -- Works too so now i want to know whats the depth is
> do local str, count = path:gsub('[%/\\][%u%l%s%_%-]+','"%1"\n') print(str) return count end
"/home"
"/dev"
"/project"
"/folder with spaces mixed with one OR MORE Capitals in should not be ignored"

4
> -- OK seems usefull lets check a windows path with it
> path='C:\\tmp\\Some Folder'
> do local str, count = path:gsub('[%/\\][%u%l%s]+','<%1>') print(str) return count end
C:<\tmp><\Some Folder>
2
> -- And that is what i mean with "many"
> -- But aware that only lower upper and space chars are handled
> -- So _ - and other chars has to be included by the pattern
> -- Like: '[%/\\][%u%l%s%_%-]+'
> path='C:\\tmp\\Some_Folder'
> do local str, count = path:gsub('[%/\\][%u%l%s%_%-]+','<%1>') print(str) return count end
C:<\tmp><\Some_Folder>
2
> path='C:\\tmp\\Some-Folder'
> do local str, count = path:gsub('[%/\\][%u%l%s%_%-]+','<%1>') print(str) return count end
C:<\tmp><\Some-Folder>
2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文