在 Lua 模式中查找字符串中的 URL
2014-5-12 1:35:57
收藏:0
阅读:237
评论:1
使用 Lua 模式匹配,我想要解析字符串并找到以下 URL:
http://www.test.com/
www.test.com/
test.com/
test-test.test.com/
斜杠可以是可选的,但如果包含,它必须能够找到嵌套的文件夹,例如:
test.com/test/
这样我就可以使用单个模式匹配来查找 URL。问题在于,我使用的所有示例要么不起作用,要么会导致《魔兽世界》永远停留在加载屏幕上,或是出现我无法独立解决的一些错误。
我不再拥有我在代码中使用的模式,因此我可以使用一个可行的模式,而不是挂钩不适当的 URL。如果需要,我会后来想出一些 。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
-- 根据 RFC 3986,URL 内可包含的所有字符,但不包括逗号、分号、撇号、等号、括号和方括号 -- (因为它们通常用作 URL 分隔符) local text_with_URLs = [[ <a href="http://www.lua.org:80/manual/5.2/contents.html">L.ua 5.2</a> [url=127.0.0.1:8080]forum link[/url] intranet links: http://test, http://retracker.local/announce [markdown link](https://74.125.143.101/search?q=Who+are+the+Lua+People%3F) long subdomain chain: very.long.name.of.my.site.co.uk auth link: ftp://user:pwd@site.com/path - not recognized yet :( ]] local domains = [[.ac.ad.ae.aero.af.ag.ai.al.am.an.ao.aq.ar.arpa.as.asia.at.au .aw.ax.az.ba.bb.bd.be.bf.bg.bh.bi.biz.bj.bm.bn.bo.br.bs.bt.bv.bw.by.bz.ca .cat.cc.cd.cf.cg.ch.ci.ck.cl.cm.cn.co.com.coop.cr.cs.cu.cv.cx.cy.cz.dd.de .dj.dk.dm.do.dz.ec.edu.ee.eg.eh.er.es.et.eu.fi.firm.fj.fk.fm.fo.fr.fx.ga .gb.gd.ge.gf.gh.gi.gl.gm.gn.gov.gp.gq.gr.gs.gt.gu.gw.gy.hk.hm.hn.hr.ht.hu .id.ie.il.im.in.info.int.io.iq.ir.is.it.je.jm.jo.jobs.jp.ke.kg.kh.ki.km.kn .kp.kr.kw.ky.kz.la.lb.lc.li.lk.lr.ls.lt.lu.lv.ly.ma.mc.md.me.mg.mh.mil.mk .ml.mm.mn.mo.mobi.mp.mq.mr.ms.mt.mu.museum.mv.mw.mx.my.mz.na.name.nato.nc .ne.net.nf.ng.ni.nl.no.nom.np.nr.nt.nu.nz.om.org.pa.pe.pf.pg.ph.pk.pl.pm .pn.post.pr.pro.ps.pt.pw.py.qa.re.ro.ru.rw.sa.sb.sc.sd.se.sg.sh.si.sj.sk .sl.sm.sn.so.sr.ss.st.store.su.sv.sy.sz.tc.td.tel.tf.tg.th.tj.tk.tl.tm.tn .to.tp.tr.travel.tt.tv.tw.tz.ua.ug.uk.um.us.uy.va.vc.ve.vg.vi.vn.vu.web.wf .ws.xxx.ye.yt.yu.za.zm.zr.zw]] local tlds = {} for tld in domains:gmatch'%w+' do tlds[tld] = true end local function max4(a,b,c,d) return math.max(a+0, b+0, c+0, d+0) end local protocols = {[''] = 0, ['http://'] = 0, ['https://'] = 0, ['ftp://'] = 0} local finished = {} -- 提取所有链接 for pos_start, url, prot, subd, tld, colon, port, slash, path in text_with_URLs:gmatch'()(([%w_.~!*:@&+$/?%%#-]-)(%w[-.%w]*%.)(%w+)(:?)(%d*)(/?)([%w_.~!*:@&+$/?%%#=-]*))' do -- 排除对已经处理过的子域处理 if protocols[prot:lower()] == (1 - #slash) * #path and not subd:find'%W%W' and (colon == '' or port ~= '' and port + 0 < 65536) and (tlds[tld:lower()] or tld:find'^%d+$' and subd:find'^%d+%.%d+%.%d+%.$' and max4(tld, subd:match'^(%d+)%.(%d+)%.(%d+)%.$') < 256) then finished[pos_start] = true print(pos_start, url) end end -- 处理其他链接 for pos_start, url, prot, dom, colon, port, slash, path in text_with_URLs:gmatch'()((%f[%w]%a+://)(%w[-.%w]*)(:?)(%d*)(/?)([%w_.~!*:@&+$/?%%#=-]*))' do if not finished[pos_start] and not (dom..'.'):find'%W%W' and protocols[prot:lower()] == (1 - #slash) * #path and (colon == '' or port ~= '' and port + 0 < 65536) then print(pos_start, url) end end输出:
13 http://www.lua.org:80/manual/5.2/contents.html 61 L.ua 82 127.0.0.1:8080 197 https://74.125.143.101/search?q=Who+are+the+Lua+People%3F 281 very.long.name.of.my.site.co.uk 133 http://test 146 http://retracker.local/announce第二行打印是因为它看起来像某个乌克兰网站 :-)