2

I'm trying to learn how to use regexps in lua but I see no results, so I'm asking for help.

I got two types of url:

1) /a/b/c/d/some,text,commas,and,so,on,FILE.dat 
2) /a/b/c/d/FILE.dat

I need to do two things:

  1. get substring with filename: FILE.dat
  2. get substring with path: /a/b/c/d/FILE.dat

I have written regex which retrieves me a filename from a first case:

string.match(url, ".*,(.*)")

similar regex retrives me a filename from second case:

string.match(url, ".*/(.*)")

Now can You tell me, how to merge this two regexs into one?

1
  • 4
    Lua doesn't have regexes, it has some pattern matching, which is a lot more limited. Commented Jul 28, 2011 at 13:41

1 Answer 1

2

If those two cases are your only ones, matching for the filename is easy starting from the back:

filename=string.match(url,'([%w_]+%.%w%w%w)$')

For tossing out the comma separated part I'd resort to something like

filepath=string.gsub(url,'%w+,', '')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.