简体   繁体   English

ruby 中的正则表达式?

[英]Regular expression in ruby?

I have a URL like below.我有一个像下面这样的网址。

/shows/the-ruby-book/meta-programming/?play=5b35a825-d372-4375-b2f0-f641a38067db"

I need to extract only the id of the play (ie 5b35a825-d372-4375-b2f0-f641a38067db ) using regular expression.我只需要使用正则表达式提取播放的 id(即5b35a825-d372-4375-b2f0-f641a38067db )。 How can I do it?我该怎么做?

I would not use a regexp to parse a url.我不会使用正则表达式来解析 url。 I would use Ruby's libraries to handle URLs:我会使用 Ruby 的库来处理 URL:

require 'uri'

url = '/shows/the-ruby-book/meta-programming/?play=5b35a825-d372-4375-b2f0-f641a38067db'

uri = URI.parse(url)
params = URI::decode_www_form(uri.query).to_h

params['play']
# => 5b35a825-d372-4375-b2f0-f641a38067db

You can do:你可以做:

str = '/shows/the-ruby-book/meta-programming/?play=5b35a825-d372-4375-b2f0-f641a38067db'
match = str.match(/.*\?play=([^&]+)/)
puts match[1]

=> "5b35a825-d372-4375-b2f0-f641a38067db"

The regex /.*\\?play=([^&]+)/ will match everything up until ?play= , and then capture anything that is not a & (the query string parameter separator)正则表达式/.*\\?play=([^&]+)/将匹配所有内容直到?play= ,然后捕获任何不是& (查询字符串参数分隔符)

A match will create a MatchData object, represented here by match variable, and captures will be indices of the object, hence your matched data is available at match[1] .匹配将创建一个MatchData对象,这里由match变量表示,捕获将是对象的索引,因此匹配的数据在match[1]处可用。

url = '/shows/the-ruby-book/meta-programming/?play=5b35a825-d372-4375-b2f0-f641a38067db'
url.split("play=")[1] #=> "5b35a825-d372-4375-b2f0-f641a38067db"

Ruby's built-in URI class has everything needed to correctly parse, split and decode URLs: Ruby 的内置URI类拥有正确解析、拆分和解码 URL 所需的一切:

require 'uri'

uri = URI.parse('/shows/the-ruby-book/meta-programming/?play=5b35a825-d372-4375-b2f0-f641a38067db')
URI::decode_www_form(uri.query).to_h['play'] # => "5b35a825-d372-4375-b2f0-f641a38067db"

If you're using an older Ruby that doesn't support to_h , use:如果您使用的是不支持to_h的旧 Ruby,请使用:

Hash[URI::decode_www_form(uri.query)]['play'] # => "5b35a825-d372-4375-b2f0-f641a38067db"

You should use URI, rather than try to split/extract using a regexp, because the query of a URI will be encoded if any values are not within the characters allowed by the spec.您应该使用 URI,而不是尝试使用正则表达式拆分/提取,因为如果任何值不在规范允许的字符范围内,则将对 URI 的查询进行编码。 URI, or Addressable::URI , will decode those back to their original values for you. URI 或Addressable::URI将为您将它们解码回其原始值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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