Jump to content
  • 0

String match help


FangYX

Question

How to get only up to the second str?

(I want the output is "str a str")

Quote

text="str a str bc str cde str"

print(string.match(text, "str.*str"))

if like that the output always show the last str

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Use "-" quantifier instead of "*": 

string.match(text, "str.-str")

From Lua reference manual (https://www.lua.org/manual/5.3/manual.html#6.4.1) :

Quote

pattern item can be

  • ...
  • a single character class followed by '-', which also matches zero or more repetitions of characters in the class. Unlike '*', these repetition items will always match the shortest possible sequence;
  • ...
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.