【问题标题】:Lua: How to get string captures containing a specific substring?Lua:如何获取包含特定子字符串的字符串捕获?
【发布时间】:2013-08-29 08:01:50
【问题描述】:

在 Lua 中,想要从包含特定子字符串的字符串中获取捕获。例如。在字符串中

 test = "<item>foo</item>  <item>bar</item>"

我想获取包含“a”的项目,在本例中为“bar”。我试过这个:

print(string.find(test, "<item>(.-a.-)</item>"))

但结果是:

1   34  foo</item>  <item>bar

所以 .- 比我预期的更贪婪。正确的模式是什么?

【问题讨论】:

  • 不要用模式解析 XML。
  • 这不是用来解析的,我正在寻找 XML 中的特定部分。

标签: string lua pattern-matching


【解决方案1】:

试试print(string.find(test, "&lt;item&gt;([^&lt;]-a.-)&lt;/item&gt;"))

【讨论】:

    猜你喜欢
    • 2020-08-26
    • 2015-01-20
    • 1970-01-01
    • 2017-08-02
    • 2021-12-31
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2012-09-16
    相关资源
    最近更新 更多