我正在创建一个基于Sinatra的应用程序,并尝试使用正则表达式解析一个长字符串以从中提取链接。
这是该字符串的摘录,其中包含我需要提取的相关信息:
time=18ms\n[INFO] Calculating CPD for 0 files\n[INFO] CPD calculation finished\n[INFO] Analysis report generated in 325ms, dir size=14 KB\n[INFO] Analysis reports compressed in 187ms, zip size=8 KB\n[INFO] Analysis report uploaded in 31ms\n[INFO] ANALYSIS SUCCESSFUL, you can browse http://sonar.company.com/dashboard/index/com.company.paas.maventestproject:MavenTestProject\n[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report\n[INFO] More about the report processing at http://sonar.company.com/api/ce/task?id=AVhFxTkyob-dgWZqnfIn\n[INFO] -----------------------------------------------------------------------
我需要能够做到以下几点:
http://sonar.company.com/api/ce/task?id=AVhFxTkyob-dgWZqnfIn
我获得的距离最近,/(?=http).[a*-z]*/
但与我需要的距离不近,因为它发现615场比赛而不是1场比赛。
问题还在于,idAVhFxTkyob-dgWZqnfIn
不是静态的,每次构建都在变化。
我一直在使用Rubular.com查找需要使用的适当正则表达式。
>> string = '[your long string here]'
>> regex = /(http:[\w\/.?=-]+)(\\n)/
>> string.scan(regex).first.first
=> "http://sonar.company.com/api/ce/task?id=AVhFxTkyob-dgWZqnfIn"
按照上面的示例,我最终将正则表达式修改为以下内容:
(http:\/\/sonar[\w\/.?=-]+task[\w\/.?=-]+(?!.\\n))
..并以如下方式返回:
string.scan(regex).first.first
我修改正则表达式的原因是因为插入完整字符串而不是OP中的摘录时,以前的正则表达式最终会产生很多结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句