From: Brian Candler Date: 2009-12-14T21:46:50+09:00 Subject: Re: problem with trivial regular expression David Villa wrote: > I posted a random string and i understand it better, but the real > question and string is : But where does this string actually come from? It looks a bit like URLs but with an extra layer of URL-encoding, in that = appears as %3D, for example, and some extra numeric prefixes like 18| So it would be much more helpful to understand what the real structure of this string is, rather than just guessing, in which case you don't need to guess about how to decode it. Removing the first level of escaping: > irb(main):007:0> CGI.unescape(s) => "\"18|http://v14.lscache3.c.youtube.com/videoplayback?ip=0.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=900034%2C902305&algorithm=throttle-factor&itag=18&ipbits=0&burst=40&sver=3&expire=1260813600&key=yt1&signature=4C33C8CB5787DBA4D111A2E66BFFACCC2E95E0A7.D302F87BF11A4BD47631704E858C762325913921&factor=1.25&id=cf9829e68818de48,34|http://v7.lscache8.c.youtube.com/videoplayback?ip\320.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=900034%2C902305&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&expire=1260813600&key=yt1&signature=158633D23CDC66CC40D171EC8CB48A84B4BCB223.86F6779A36E14684B367BCCF91588556A871C421&factor=1.25&id=cf9829e68818de48,5|http://v16.lscache8.c.youtube.com/videoplayback?ip=0.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgrithm%2Cburst%2Cfactor&fexp=900034%2C902305&algorithm=throttle-factor&itag=5&ipbits=0&burst=40&sver=3&expire=1260813600&key=yt1&signature=91FAC179A7C02DD66 2942CCEB71D8BE05BD7B5D3.BC4BFC97F8BA02519CC817D2541E5D0548BC2C7C&factor=1.25&id=cf9829e68818de48\"" So my total *guess* is that this is a double-quoted string, which contains comma-separated fields, and each field is of the form nn|URL. In which case you can unwrap it in stages: irb(main):011:0> s.sub!(/\A"(.*)"\z/) { $1 } irb(main):012:0> fields = CGI.unescape(s).split(',') irb(main):013:0> fields.each { |f| num,url = f.split('|',2); puts "***",url }; nil *** http://v14.lscache3.c.youtube.com/videoplayback?ip=0.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=900034%2C902305&algorithm=throttle-factor&itag=18&ipbits=0&burst=40&sver=3&expire=1260813600&key=yt1&signature=4C33C8CB5787DBA4D111A2E66BFFACCC2E95E0A7.D302F87BF11A4BD47631704E858C762325913921&factor=1.25&id=cf9829e68818de48 *** http://v7.lscache8.c.youtube.com/videoplayback?ip�.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=900034%2C902305&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&expire=1260813600&key=yt1&signature=158633D23CDC66CC40D171EC8CB48A84B4BCB223.86F6779A36E14684B367BCCF91588556A871C421&factor=1.25&id=cf9829e68818de48 *** http://v16.lscache8.c.youtube.com/videoplayback?ip=0.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgrithm%2Cburst%2Cfactor&fexp=900034%2C902305&algorithm=throttle-factor&itag=5&ipbits=0&burst=40&sver=3&expire=1260813600&key=yt1&signature=91FAC179A7C02DD662942CCEB71D8BE05BD7B5D3.BC4BFC97F8BA02519CC817D2541E5D0548BC2C7C&factor=1.25&id=cf9829e68818de48 => nil IMO it's far better to use the structure of the input to delimit the data you're looking for, rather than guessing where the start and end of each datum is based on what you expect the datum to look like. -- Posted via http://www.ruby-forum.com/.