Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在字符串lua模式中查找url_Url_Lua_Pattern Matching_String Matching_Lua Patterns - Fatal编程技术网

在字符串lua模式中查找url

在字符串lua模式中查找url,url,lua,pattern-matching,string-matching,lua-patterns,Url,Lua,Pattern Matching,String Matching,Lua Patterns,使用Lua模式匹配,我希望能够解析字符串并找到以下URL http://www.test.com/ www.test.com/ test.com/ test-test.test.com/ 斜杠可以是可选的,但如果包含斜杠,则必须能够找到嵌套文件夹,例如: test.com/test/ 这样,我就可以使用单个模式匹配来查找url。问题是,我使用的所有示例要么不起作用,要么导致魔兽世界永远不会离开加载屏幕,出现一些我自己无法解决的错误 我不再有我在代码中使用的模式,所以我可以使用一个可以工作并且

使用Lua模式匹配,我希望能够解析字符串并找到以下URL

http://www.test.com/
www.test.com/
test.com/
test-test.test.com/
斜杠可以是可选的,但如果包含斜杠,则必须能够找到嵌套文件夹,例如:

test.com/test/
这样,我就可以使用单个模式匹配来查找url。问题是,我使用的所有示例要么不起作用,要么导致魔兽世界永远不会离开加载屏幕,出现一些我自己无法解决的错误

我不再有我在代码中使用的模式,所以我可以使用一个可以工作并且不会钩住不正确的URL的模式。如果以后需要的话,我会想出一些

-- all characters allowed to be inside URL according to RFC 3986 but without
-- comma, semicolon, apostrophe, equal, brackets and parentheses
-- (as they are used frequently as URL separators)
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

第二行被打印出来是因为它看起来像是乌克兰的网站:-)

这太棒了,现在我该如何抓取位置或交换位置,在捕获的文本的结尾和开头添加内容?基本上我需要起点和文本。然后我将使用交换添加自定义文本到魔兽世界中的超链接。你能帮我理解这个代码是如何工作的并给我解释一下吗?我想能够知道如何提出这样的东西,我自己的情况下,我想做一个不同的格式后。答案更新。将显示找到的文本的位置。谢谢。我真的很感激。你应该已经看到了我的代码没有像这样的lol方法。我会在代码和插件中列出你的信用为这一点。再次感谢。我看到你再次更新了它。。。你太过分了,男人。。。我应该先来这里lol。我希望我能投票,但我没有代表。显然下面的答案也适用于这一点。它必须是上述所有内容和任何形式的有效URL。我可以自己解析#:#:#:#:#,因为它有一个简单的公式,但我对URL本身有问题。
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