简体   繁体   English

string.gmatch查找两个不等号之间的字符串

[英]string.gmatch to find a string included between two inequality signs

I'm using Lua, already used Google and nothing, can't find way to get string between inequality signs (< >). 我正在使用Lua,已经使用过Google,什么也没有,找不到在不等号(<>)之间得到字符串的方法。 Other brackets are easy to get but these not. 其他括号很容易获得,但这些不是。 It's possible to do? 有可能吗?

Target: How to grab "name" from string between inequality signs? 目标:如何从不等号之间的字符串中获取“名称”?

String: < name >: Message 字符串:<name>:消息

如果name不包含> ,则<(.-)>起作用。

You can use the (%b<>) pattern to capture matching <> . 您可以使用(%b<>)模式捕获匹配的<> Then using that value, you can simply use string.sub to cut off the first and last char: 然后使用该值,您可以简单地使用string.sub来切断第一个和最后一个char:

name,message=('< name<> > : Foo Bar!'):match('(%b<>)%s*:%s*(.*)')
name=name:sub(2,-2)
print(name,'sent message :',message)

As you can see this also takes care of strings containing other, embedded <> signs 正如您所看到的,这也会处理包含其他嵌入式<>符号的字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM