user2727133
user2727133

Reputation: 309

Lua: How to get string captures containing a specific substring?

In Lua, in want to get captures from a string containing a specific substring. E.g. in the string

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

I want to get items containing "a", which in this case would be "bar". I tried this:

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

but the result is:

1   34  foo</item>  <item>bar

So .- is more greedy than I expected. What would be the correct pattern?

Upvotes: 2

Views: 275

Answers (1)

lhf
lhf

Reputation: 72312

Try print(string.find(test, "<item>([^<]-a.-)</item>")).

Upvotes: 1

Related Questions