Doc Kodam
Doc Kodam

Reputation: 781

How to split a string before a dot in Lua?

I need to do a simple split of a string.

The string is "That.Awkward.Moment.2014.720p.BluRay.x264.YIFY.srt"

I just need "That.Awkward.Moment.2014.720p.BluRay.x264.YIFY" without ".srt"

I tried this and is wrong:

print(string.match("That.Awkward.Moment.2014.720p.BluRay.x264.YIFY.srt", '^.-.s'))

How would I do it?

Upvotes: 2

Views: 1511

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26744

Since regular matching is greedy, you just need to match anything until you see . (don't forget to escape it):

print(string.match("That.Awkward.Moment.2014.720p.BluRay.x264.YIFY.srt", '(.+)%.(.+)'))

will print

That.Awkward.Moment.2014.720p.BluRay.x264.YIFY  srt

Upvotes: 5

Related Questions