1

I am trying to extract url with match. I'm trying to find the filename after the "/" character. But url is always variable so i have to start from end

123, 123.py, 123.dat

url://xxxxx/yyyyy/123
url://xxxxx/yyyyy/123.py
url://xxxxx/yyyyy/123.dat

I tried url:match("^.+(%..+)$") but I can't access files that don't start with a ..

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
sweetngx
  • 67
  • 4

2 Answers2

1

You need

url:gsub(".*/", "")

The pattern matches

  • .* - zero or more chars as many as possible
  • / - a / char.

To find the directory, you can use

url:gsub("/[^/]*$", "")

Here,

  • / - a / char
  • [^/]* - zero or more chars other than /
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

Here's the alternative using match:

url:match"[^/]+$"

Explanation: Match one or more non-slash characters anchored to the end of string $.

This will also match just a filename like foo.bar without any slashes. If you want to force slashes to precede your filename, use /(.-)$ instead.

Luatic
  • 8,513
  • 2
  • 13
  • 34