Jump to content
  • 0

String match help


Fathoni267
 Share

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

  • 0

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;
  • ...
Edited by CmP
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • 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.