简体   繁体   中英

How to remove all characters before last matching character in Lua

So i'm a little bit confused about Lua pattern matching. I have a script to log history of videos played with MPV, but i don't need full path there, just filename is enough, so i wanted to remove all everything before last / char. In Python i would do simple

filename = re.sub(r'.*/', '', path)

But i failed horribly using Lua pattern matching. How does one do that?

Example input with desirable output:

>>> path = "/some/path/to/file.mkv"
>>> filename = re.sub(r'.*/', '', path)
>>> filename
'file.mkv'

Seems i have figured it out by myself, oh well. Used this kind of pattern:

path = "/path/to/some/file"
filename = path:gsub(".*/", "")

filename now contains only "file".

EDIT: As suggested, i changed path:gsub("(.*).*/",'') to path:gsub(".*/", "") instead since it's much more readable syntax .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM