2016-11-09 53 views
0

我正在创建一个基于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.

问题还在于,身份证AVhFxTkyob-dgWZqnfIn不是stat ic,每个版本更改。

我一直在使用Rubular.com找到我需要使用的正确的正则表达式。

+0

这有什么好处:'/ http:\/\/\ S + /'?请参阅[这里](http://www.rubular.com/r/aHG7BZZAmw)进行演示。 –

+0

而不是加载一个字符串中的整个文件,逐行阅读它。这样,您可以快速放弃不以'[INFO]'开头的行。然后用'\ bhttp:// \ S *'检查这一行并解析url。 –

+0

您需要说明用于识别您希望提取的字符串的规则。在你的例子中,你显示你想要的字符串,但不要告诉我们*为什么*它是那个特定的字符串。这就像是说你有一组数字[3,5,8,12,13,20]'并且想知道如何选择'[3,5,13]'。是因为他们是奇数,素数还是别的什么?你需要用问题的陈述开始你的问题,然后在适当的时候提供一个例子。当您修改以澄清时,请不要添加“编辑:”。最后,未来,请将您的例子归结为最基本的要领。 –

回答

2
>> 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中的摘录时以很多结果结束。

+0

这几乎可以工作(因为字符串在它也与之匹配之前还包含链接)..我将它修改为看起来像这样:/(http:\/\/sonar [\ w \ /。?= - ] +)( \\ n)/'但是这也匹配另一个链接(它在'******** -Dsonar.host.url = http://sonar.covisintrnd.com \ n [INFO]')..无论如何告诉它,它必须包含像'task'或'id ='或类似的东西? – Fadi

+0

嗯,奇怪..当我在我的应用程序尝试它时,它给了我以下错误:错误NoMethodError:未定义的方法'第一'为零:NilClass'我猜它没有返回任何东西,因此它是零.. – Fadi

+0

This正则表达式工作(在rubular.com上):'(http:\/\/sonar [\ w \ /。?= - ] +)任务[\ w \ /。?= - ] +(?!。\\ n) '但现在它实际上正在返回(在实际的红宝石应用程序中):'[[“http://sonar.company.com/api/ce/”]] ...嗯 – Fadi

1

有很好的测试工具,可以让你的工作更轻松。我推荐使用URI的extract方法:

require 'uri' 

str = "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] -----------------------------------------------------------------------" 

URI.extract(str) 
# => ["http://sonar.company.com/dashboard/index/com.company.paas.maventestproject:MavenTestProject", 
#  "http://sonar.company.com/api/ce/task?id=AVhFxTkyob-dgWZqnfIn"] 

然后,它找到你想要的链接,并使用它的一个简单的事情。

您还需要注意URI为该方带来的所有其他方法,因为它了解如何根据RFC分解和构建URI。

不要推出自己的代码或正则表达式来完成别人已经完成的工作,特别是当代码经过良好测试时。你会避免别人会陷入的陷阱。 URI的作者/维护者管理内置模式,所以我们不需要。而且,它比你想象的要复杂得多,比如:

URI::REGEXP::PATTERN::ABS_URI 
"[a-zA-Z][\\-+.a-zA-Z\\d]*:(?:(?://(?:(?:(?:[\\-_.!~*'()a-zA-Z\\d;:&=+$,]|%[a-fA-F\\d]{2})*@)?(?:(?:[a-zA-Z0-9\\-.]|%\\h\\h)+|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\[(?:(?:[a-fA-F\\d]{1,4}:)*(?:[a-fA-F\\d]{1,4}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(?:(?:[a-fA-F\\d]{1,4}:)*[a-fA-F\\d]{1,4})?::(?:(?:[a-fA-F\\d]{1,4}:)*(?:[a-fA-F\\d]{1,4}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}))?)\\])(?::\\d*)?|(?:[\\-_.!~*'()a-zA-Z\\d$,;:@&=+]|%[a-fA-F\\d]{2})+)(?:/(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*)*(?:/(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*)*)*)?|/(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*)*(?:/(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*)*)*)(?:\\?(?:(?:[\\-_.!~*'()a-zA-Z\\d;/?:@&=+$,\\[\\]]|%[a-fA-F\\d]{2})*))?|(?:[\\-_.!~*'()a-zA-Z\\d;?:@&=+$,]|%[a-fA-F\\d]{2})(?:[\\-_.!~*'()a-zA-Z\\d;/?:@&=+$,\\[\\]]|%[a-fA-F\\d]{2})*)" 
相关问题