Re: Regex to match valid host or dns names
> example below, it fails to match "host-no.top-level" as a valid host > name. I modify the regex several times - but still don't get the right > outlook. > > my @hosts = qw(192.168.22.1 192.168.22.18 localhost another.host.domain > host-no.top-level my.host.domain.com); > foreach (@hosts){ > # Works ok > push (@ips, $_ ) if $_ =~ /^\d{1,3}\.\d{1,3}\.\d{1|3}/; > > # Can't match "host-no.top-level". > push (@dns, $_) if $_ =~ /^\w+-?[\w+]?\.?[\w+.{1}]*\w+$/; > } /^\w+-?[\w+]?\.?[\w+.{1}]*\w+$/-->Here you look for only one "-" and also not allowing any other non-word charaters(like hyphen). The "." can match any character even other than "-" . You can think like this:(For IP's) search for a number with maximum 3 digits and then followed by the same kind of 3 numbers but prefixed with a dot. Try this ---> $_ =~ /^\d{1,3}[\.\d{1,3}]{3}/ You can think like this:(For DNS's) search for a WORD which may(-?) contain hyphen within it and then followed by the same kind of zero-or-more-WORDs but prefixed with a dot which is a normal dns name pattern. Try this > $_ =~ /^\w\w*-?\w+?[\.\w\w*-?\w+?]*$/ But this will allow IP's also in your "@dns" because \w can match digits also. -- Regards, K.Prabakar -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
RE: Regex to match valid host or dns names
On Thu, 14 Oct 2004, K.Prabakar wrote: > On Wed, 13 Oct 2004, Babale Fongo wrote: > > > K.Prabakar's suggestion looks good but also failed the test: > > > > "$_ =~ /^\w\w*-?\w+?[\.\w\w*-?\w+?]*$/", > > > > It will match an invalid dns name like this (host-.domain.com) as a valid. > > I'm still working on it, but will welcome any other suggestion. > > > > Babs > > As "Randal L. Schwartz" pointed out I used character class"[]" in the above regex which is wrong. That should be "()" like this--> /^\w\w*-?\w+?(\.\w\w*-?\w+?)*$/ Now it won't match "host-.domain.com" like names. Other thing is this will allow dns names starting with DEGITS.In that case the worst case solution will be /^[a-zA-Z]\w*-?\w+?(\.[a-zA-Z]\w*-?\w+?)*$/ . This will allow names like "bla-3bla.bla" , "bla-bla" but won't allow "3bla.bla" and "bla.4bla" To avoid underscore again the worst case will be to replace \w with [-a-zA-Z] in the above expression. That won't look good at all. -- Regards, K.Prabakar -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
Re: Need Help
Within single quotes "[" is not considered as a special character. So,it's a mess trying to escape this character with backslash "\". So,your code expects \[%one_two%\] instead of [%one_two%]. On Thu, 7 Oct 2004, Anish Kumar K. wrote: > Hi > > Please anyone help me in Reg Exp. I wanted to replace "[%one_two%]" and "[%pne%]" > with the value "New" say... > #!/usr/bin/perl > > $openTag='\[%'; > $closeTag='%\]'; > my $count=0; > $_= "This is a test for [%one_two%] number and [%pne%] numbers."; > > s/$openTag.*$closeTag/New/g; > > print "The new line is:: $_ \n"; > -- Regards, K.Prabakar -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>