如何在lua中检查url是否有效?
问题描述:
我想检查 url 是否有效,在 lua 中正确的正则表达式是什么?我试过这样的正则表达式
I want to check if url is valid, what is the correct regexp to do that in lua? I tried regexp like this
string.match('https://stackoverflow.com/', '[a-z]*:\/\/[^ >,;]*')
但出现错误
invalid escape sequence near ''[a-z]*:\/'
更新:
string.match('https://stackoverflow.com/', '[a-z]*://[^ >,;]*')
正确答案
答
错误很明显:\/
是无效的转义.您不需要转义 /
,因为它不是 Lua 模式中的特殊字符 (检查魔法"字符列表)并删除转义符应该可以工作:string.match('https://stackoverflow.com/', '[az]*://[^ >,;]*')
.
The error is fairly clear: \/
is an invalid escape. You don't need to escape /
, as it's not a special character in Lua patterns (check the list of "magic" characters) and removing the escape should work: string.match('https://stackoverflow.com/', '[a-z]*://[^ >,;]*')
.