简体   繁体   中英

Ruby regex match then exclude from match

I have a URL like so:

http://www.example.com/blah.ashx?ID=fj203-fj230r-3jf030

I was wondering if it's possible to match everything from the ID= to the end of the URL in Ruby, then exclude the ID= from the match? Thanks!

Ruby probably has a built in URL and query-string parser, but this regex should work simply enough:

\?ID=(.*)

http://rubular.com/r/kYf3EURRPN

I always stick with library methods, lets me sleep better ;-)

require 'cgi'
require 'uri'

uri    = URI.parse('http://www.example.com/blah.ashx?ID=fj203-fj230r-3jf030')
params = CGI.parse(uri.query)
id     = params["ID"].first

#=> "fj203-fj230r-3jf030"

这样的事情。

"http://www.example.com/blah.ashx?ID=fj203-fj230r-3jf030"[/(?<=ID=)(.*)/]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM