Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2958

Re: Extract a range i.e. svr?

From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: Extract a range i.e. svr?
Date 2011-04-15 12:36 -0500
Organization Service de news de lacave.net
Message-ID <179b1d9eb1df11dd8b91b81c3a6faabb@ruby-forum.com> (permalink)
References <a09ed402a715adbb77804d95cdf94bf2@ruby-forum.com> <BANLkTikF=HrdQnu9DQTnp-Buzr6dtnMjLQ@mail.gmail.com>

Show all headers | View raw


"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> 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/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 11:08 -0500
  Re: Extract a range i.e. svr[100..130] ? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-15 11:41 -0500
    Re: Extract a range i.e. svr? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-15 12:36 -0500
  Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 13:24 -0500
    Re: Extract a range i.e. svr[100..130] ? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-15 13:27 -0500
  Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 13:40 -0500
    Re: Extract a range i.e. svr[100..130] ? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-15 15:50 -0500
  Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 13:46 -0500
  Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 15:11 -0500
    Re: Extract a range i.e. svr[100..130] ? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-18 15:33 -0500
  Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 16:48 -0500
  Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 16:54 -0500
    Re: Extract a range i.e. svr[100..130] ? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-15 17:03 -0500

csiph-web