如何使用正则表达式从字符串中提取子字符串?

时间:2023-01-25 19:19:52

I am new to regular expressions in Ruby.

我对Ruby中的正则表达式不熟悉。

The string looks something like http://www.site.com/media/pool/product_color_purple.jpg and I am trying to extract from this just the bit which has the colour in it. This can be a variable length, as some of the colours are like prince_purple.jpg.

这个字符串看起来像http://www.site.com/media/pool/product_color_purple.jpg,我正试图从其中提取有颜色的位。这可以是一个可变长度,因为有些颜色像普林。

So I have:

所以我有:

colour = c.attr('src').match(/(.*)color_(.*).jpg/)
puts "Colour is #{colour}"

What colour returns is the string again, instead of the extracted bit, which is the colour. What is going wrong here?

返回的颜色是字符串,而不是提取的位,也就是颜色。这里出了什么问题?

5 个解决方案

#1


13  

str="http://www.site.com/media/pool/product_color_purple.jpg"
colour = str.match(/color_([^\/.]*).jpg$/)
puts "Colour is #{colour[1]}"

You not get "Colour is purple" because match returns MatchData, not string

你不会得到“颜色是紫色的”,因为match返回的是匹配数据,而不是字符串

#2


3  

url="http://www.site.com/media/pool/product_color_purple.jpg"
color = url.scan(/color_(.*).jpg/)[0][0]
#=> purple

or

url="http://www.site.com/media/pool/product_color_purple.jpg"
color = url.match(/color_(.*).jpg/)[1]
#=> purple

#3


2  

Without Regexp as an example of another way to do it

没有Regexp作为另一种方法的示例

url="http://www.site.com/media/pool/product_color_purple.jpg"
color = url[url.rindex("_")+1..-1].split(".")[0]

For this I would stick with regexp though.

为此,我将坚持使用regexp。

color = url.match(/.*_(.*)\./)[1]

#4


0  

>> s = %w(http://www.site.com/media/pool/product_color_purple.jpg http://www.site.com/media/pool/product_color_prince_purple.jpg) 
#=> ["http://www.site.com/media/pool/product_color_purple.jpg", "http://www.site.com/media/pool/product_color_prince_purple.jpg"]
>> s.map { |c| c.match(/\w*_color_(\w+).jpg/)[1] } 
#=> ["purple", "prince_purple"]

#5


0  

You can try this regex.

你可以试试这个regex。

/color_(.*)?.jpg/

#1


13  

str="http://www.site.com/media/pool/product_color_purple.jpg"
colour = str.match(/color_([^\/.]*).jpg$/)
puts "Colour is #{colour[1]}"

You not get "Colour is purple" because match returns MatchData, not string

你不会得到“颜色是紫色的”,因为match返回的是匹配数据,而不是字符串

#2


3  

url="http://www.site.com/media/pool/product_color_purple.jpg"
color = url.scan(/color_(.*).jpg/)[0][0]
#=> purple

or

url="http://www.site.com/media/pool/product_color_purple.jpg"
color = url.match(/color_(.*).jpg/)[1]
#=> purple

#3


2  

Without Regexp as an example of another way to do it

没有Regexp作为另一种方法的示例

url="http://www.site.com/media/pool/product_color_purple.jpg"
color = url[url.rindex("_")+1..-1].split(".")[0]

For this I would stick with regexp though.

为此,我将坚持使用regexp。

color = url.match(/.*_(.*)\./)[1]

#4


0  

>> s = %w(http://www.site.com/media/pool/product_color_purple.jpg http://www.site.com/media/pool/product_color_prince_purple.jpg) 
#=> ["http://www.site.com/media/pool/product_color_purple.jpg", "http://www.site.com/media/pool/product_color_prince_purple.jpg"]
>> s.map { |c| c.match(/\w*_color_(\w+).jpg/)[1] } 
#=> ["purple", "prince_purple"]

#5


0  

You can try this regex.

你可以试试这个regex。

/color_(.*)?.jpg/