From: Peter Zotov Date: 2012-09-10T09:13:31+09:00 Subject: Re: Need help with my logic sharmin malik писал 10.09.2012 01:55: >> unless myname =~ /\A(\w+-v)([khv])(-\d+\..+)\z/ >> raise "Bad hostname format: #{myname.inspect}" >> end >> hosts = [$1+"k"+$3, $1+"h"+$3, $1+"v"+$3] >> unless hosts.delete myname >> raise "Can't find my hostname #{myname.inspect} to remove from >> #{hosts.inspect}" >> end >> hosts.each do |host| >> puts "Please connect to #{host}" >> end > > HI this is nice way to do .. > but system hostname is like this (i forgot put -dk ) > > dk-vk-2.-dk.domain.lan > > I tried to change the Regix.. but not working > Can you please modify it little bit ?? > also would be able to explain me little bit how the regix is related > to > [$1+"k"+$3, $1+"h"+$3, $1+"v"+$3] ?? > thanks Parts of strings which are matched by capturing groups (parenthesized groups of symbols in regexen) are assigned to $-variables. For example, look at this code: "test 123 abc" =~ /test (..)3 ([ba]+)/ puts "$1,$2" # outputs 12,ab The $N numeric variables are function-local and always refer to the last regex executed in the current function. There is an effectively infinite number of them. You can make regex groups non-capturing by placing ?: in the beginning of the group. E.g. ([ba]+) is capturing, and (?:[ba]+) is non-capturing. Non-capturing groups behave in exactly same way as capturing ones, but they do not have a corresponding $N-variable. -- WBR, Peter Zotov.