Re: Regex to match valid host or dns names

2004-10-13 Thread K.Prabakar

> 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]
 




Re: Regex to match valid host or dns names

2004-10-13 Thread Steve Bertrand
>
>> 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+$/;
>> }

I'm just beginning to learn a bit about some of the more obscure
regex's, but I'd like to ask if this following regex would ensure no
IP's got trapped in the @dns array? (Assuming that no .tld ends in a
\d):

push (@dns, $_) if $_ =~ /^\w+-?[\w+]?\.?[\w+.{1}]*[a-zA-Z]{2,3}$/;

Steve




>
>
>
>
>  /^\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]
>  
>
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Regex to match valid host or dns names

2004-10-13 Thread Randal L. Schwartz
> "Steve" == Steve Bertrand <[EMAIL PROTECTED]> writes:

Steve> I'm just beginning to learn a bit about some of the more obscure
Steve> regex's, but I'd like to ask if this following regex would ensure no
Steve> IP's got trapped in the @dns array? (Assuming that no .tld ends in a
Steve> \d):

Steve> push (@dns, $_) if $_ =~ /^\w+-?[\w+]?\.?[\w+.{1}]*[a-zA-Z]{2,3}$/;

This is wrong because it uses \w repeatedly, not [-0-9a-zA-Z], which
is needed.  Underscore is not legal in hostname parts.  Dash is.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Regex to match valid host or dns names

2004-10-13 Thread Randal L. Schwartz
> "K" == K Prabakar <[EMAIL PROTECTED]> writes:

K> Try this > $_ =~ /^\w\w*-?\w+?[\.\w\w*-?\w+?]*$/

"Try" is usually a good clue to mean "This is not your solution".

In this case, you've got all sorts of stuff going on in a character class.
Wrong stuff.  "Try" again.  Please.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Regex to match valid host or dns names

2004-10-13 Thread Babale Fongo

Hi,

My original regex to match ips is this:
 "$_ =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}/;", which is ok. But matching dns name
is still a problem. 

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

||-Original Message-
||From: Steve Bertrand [mailto:[EMAIL PROTECTED]
||Sent: Wednesday, October 13, 2004 5:24 PM
||To: K.Prabakar
||Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED]
||Subject: 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+$/;
||>> }
||
||I'm just beginning to learn a bit about some of the more obscure
||regex's, but I'd like to ask if this following regex would ensure no
||IP's got trapped in the @dns array? (Assuming that no .tld ends in a
||\d):
||
||push (@dns, $_) if $_ =~ /^\w+-?[\w+]?\.?[\w+.{1}]*[a-zA-Z]{2,3}$/;
||
||Steve
||
||
||
||
||>
||>
||>
||>
||>  /^\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>
||>
||>
||>
||
||
||
||--
||To unsubscribe, e-mail: [EMAIL PROTECTED]
||For additional commands, e-mail: [EMAIL PROTECTED]
||<http://learn.perl.org/> <http://learn.perl.org/first-response>




-- 
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

2004-10-13 Thread Wiggins d Anconia
 
> Hi,
> 
> My original regex to match ips is this:
>  "$_ =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}/;", which is ok. But matching dns name
> is still a problem. 
> 

For some definitions of "ok". Seen an IP like: 987.654.321.0 ??  Or how
about, 255.255.255.255.1.3.4.5.6 ?? I am assuming you had at least four
of those C<\d{1,3}\.> in the mix and hopefully a trailing C<$>.

If you really want to match IPs you might consider checking out the
excellent Mastering Regex book, 

(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.

Gives you a hint, so cool. You may also be interested in the
Regexp::Common suite.



http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Regex to match valid host or dns names

2004-10-13 Thread Dan Jones
On Wed, 2004-10-13 at 15:06, K.Prabakar wrote:
> > 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.

Isn't this easily solved?

foreach (@hosts){
 if($_ =~ /^\d{1,3}[\.\d{1,3}]{3}/) {
  push (@ips, $_ );
 }
 elsif($_ =~ /^\w\w*-?\w+?[\.\w\w*-?\w+?]*$/) {
  push (@dns, $_) 
 }
}




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Regex to match valid host or dns names

2004-10-13 Thread K.Prabakar
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]
 




Re: RE: Regex to match valid host or dns names

2004-10-14 Thread perl

Your regex did not allow (w2k-server-proxy2) a valid dns name.
I now see how complex it is to match valid ips or dns names.

The more I try to modify the regex to perfection, the more I see
hopholes. So I think it is not worthwhile spending lot of time on this
regex. My script does more than pattern matching, so I will be ok with
the regex I've got so far.

Unfortunately this will also match 999.999.999.999, but I will keep it.
$_ =~ /[^0][\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.][\d]{1,3}$/; 

This matches dns to an acceptable extend.

$_ =~ /^\w+[-\w+-?\w+]*\.?\w+?\d*$/ && $_ != /^\d*$|\d{2,}/;



Thanks anyway for your help.






"K.Prabakar" <[EMAIL PROTECTED]> schrieb am 14.10.2004, 06:49:16:
> 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]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]