Search code examples
stringlualua-patterns

Find a pattern in given string of URLs in Lua


In my Lua code I am receiving lot of URLs in form of string.

Example :

['http://www.abc.com/home/', 'http://www.abc.com/', https://www.xyz.com/v/123443/css/' , http://www.xyz.com/css/' ]

I want to fetch those URLs which are like :
https://www.xyz.com/v/123443/css/ where v is pre-defined string pattern and 123443 is random version generated to URL.

Please help me to fetch all URLs which are having that pattern into it like :
"/v/12332323/"


Solution

  • str = "https://www.xyz.com/v/123443/css/"    
    print(str:match("https?://www%.[^/]+(/v/%d+/)%w+"))
    

    Output: /v/123443/

    This pattern matches strings that starts with http or https, and then ://, the website name starting with www., a /, the pre-defined string v and "random" numbers, followed by / and other stuff.