From: 7stud -- Date: 2011-04-16T02:36:08+09:00 Subject: Re: Extract a range i.e. svr? "Jesús Gabriel y Galán" wrote in post #993035: > Sorry, if I didn't understand this well. You have a string containing > "svr[100..130].domain.local" and you want: > > svr100.domain.local > svr101.domain.local > .... > svr130.domain.local > > ? > > If that's the case, then this might work: > > a = "svr[100..130].domain.local" > m = a.match(/(.*?)\[(\d+)\.\.(\d+)\](.*)/) > (m[2].to_i..m[3].to_i).each {|num| puts "#{m[1]}#{num}#{m[4]}"} > Here's my version: str = "svr[100..130].domain.local" range_pattern = / \[ #a literal opening bracket (\d+) #capture a series of one or more digits [.]{2} #two literal periods (\d+) #capture a series of one or more digits \] #a literal closing bracket /xms before_range, the_range, after_range = str.partition(range_pattern) start_range, end_range = $1, $2 start_range.upto(end_range) do |i| puts "#{before_range}#{i}#{after_range}" end --output:-- svr100.domain.local svr101.domain.local svr102.domain.local svr103.domain.local svr104.domain.local svr105.domain.local svr106.domain.local svr107.domain.local svr108.domain.local svr109.domain.local svr110.domain.local svr111.domain.local svr112.domain.local svr113.domain.local svr114.domain.local svr115.domain.local svr116.domain.local svr117.domain.local svr118.domain.local svr119.domain.local svr120.domain.local svr121.domain.local svr122.domain.local svr123.domain.local svr124.domain.local svr125.domain.local svr126.domain.local svr127.domain.local svr128.domain.local svr129.domain.local svr130.domain.local -- Posted via http://www.ruby-forum.com/.