如何在lua字符串中找到重复字符的索引
假设你有这样的路 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几种方法可以做到这一点。也许最简单的是使用
()
模式元素,该模式元素产生的匹配位置与string.gmatch
:它
按预期进行打印。要走的另一种方法(需要更多代码)将反复调用
string.find
,始终通过启动索引。假设您可能想将字符串划分为斜线,则使用
string.gmatch
:(字符串,最大长度不包含斜线的所有子字符)
该模式找到所有非零的子 IS 在这里。您可能想查看“捕获”小节。
There are several ways to do this. Perhaps the simplest is using the
()
pattern element which yields a match position combined withstring.gmatch
:which prints
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
:(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".
有很多方法。
也很高兴知道LUA已将所有字符串函数连接为方法。
那就是@lmd用
:
直接在字符串上演示的内容。我最喜欢尝试使用模式及其捕获的复杂/困难的地方的地方是Lua独立控制台:
制作Linux-Readline
因此,让我们玩模式
'[%/\\] [%u%l%s]+'
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]+'