2013-01-14 149 views
0

我有格式的字符串,字符串操作

/d.phpsoft_id=369242&url=http://f.1mobile.com/mobile_software/finance/com.mshift.android.achieva_2.apk 

,我需要使用正则表达式的结果字符串应该从http开始编辑这个字符串:即resultatnt字符串应该是

http://f.1mobile.com/mobile_software/finance/com.mshift.android.achieva_2.apk 

请帮助

回答

0
str = "/d.phpsoft_id=369242&url=http://f.1mobile.com/mobile_software/finance/com.mshift.android.achieva_2.apk" 

str.spl­it("url=")­[1] 
+0

这将打破,如果其他地方还有另外一个'url ='。 – arnab

+0

你会传递多个url参数?请定义要求。 – 99miles

1

对于这些类型的情况下,我宁愿去与容易AV有助于提供解决方案的工具,或者至少可以指引我朝着正确的方向发展。我最喜欢的正则表达式是txt2re,因为它会以许多语言输出示例代码,包括ruby。

通过解析器运行您的字符串并选择httpurl进行匹配,它output后:

txt='/d.phpsoft_id=369242&url=http://f.1mobile.com/mobile_software/finance/com.mshift.android.achieva_2.apk' 

re1='.*?' # Non-greedy match on filler 
re2='((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))' # HTTP URL 1 

re=(re1+re2) 
m=Regexp.new(re,Regexp::IGNORECASE); 
if m.match(txt) 
    httpurl1=m.match(txt)[1]; 
    puts "("<<httpurl1<<")"<< "\n" 
end 
+0

如果参数中有多个url,该怎么办?你怎么知道你想要第一场比赛? – 99miles

0

简单的答案

你需要做以下

str = "/d.phpsoft_id=369242&url=http://f.1mobile.com/mobile_software/finance/com.mshift.android.achieva_2.apk" 

start=str.index('http://') 

resultant=str[start,str.length]