Lua模式匹配:指定要匹配的模式的问题
我正在尝试在 Lua 中进行一些模式匹配,但遇到了一个小问题。我试图匹配从数据中的第一个换行符到以下模式 _\x0C
的所有内容。
这是有问题的代码:
configmatch = string.match(response, "\n(.+)(['_\x0C'])")
它似乎有时可以工作,有时它会“缩短”预期输出。问题可能与此有关: (['_\x0C']) 但我一直无法解决它。有谁知道如何解决这个问题?
I am attempting some pattern matching in Lua and have hit a small problem. I am trying to match everything from the first newline character in my data up to the following pattern _\x0C
.
here is the code that has the problem:
configmatch = string.match(response, "\n(.+)(['_\x0C'])")
it seems to be working some of the time, other times it is "cutting short" the expected output. the problem is probably to do with this: (['_\x0C']) but i have been unable to resolve it. Does anyone know how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望
_\x0C
字面地出现在字符串中,则需要使用"\n(.-_\\x0C)"
。如果您的意思是下划线后跟换页,请使用“\n(.-_\012)”
,因为没有\ x
在 Lua (5.1) 中转义。If you want
_\x0C
literally in the string, you need to use"\n(.-_\\x0C)"
. If you mean underscore followed by formfeed, use"\n(.-_\012)"
, because there are no\x
escapes in Lua (5.1).