Search code examples
stringlualua-patterns

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?


Solution

  • 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