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 [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => [ref] => <27349158$1097671073416d21a133b5c1.57540341@config2.schlund.de> [htmlstate] => on_nl2br [postusername] => K.Prabakar [ip] => kprabakar@nmswo [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 2 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) -->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: org > For additional commands, e-mail: org > > > >[/ref] [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <3505.209.167.16.15.1097681031.squirrel@209.167.16.15> [ref] => <27349158$1097671073416d21a133b5c1.57540341@config2.schlund.de> [htmlstate] => on_nl2br [postusername] => Steve [ip] => iaccounts@ibcte [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 3 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) -->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.[/ref] 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, $_) } } [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <1097681160.3834.60.camel@merlin.amber.local> [ref] => <27349158$1097671073416d21a133b5c1.57540341@config2.schlund.de> [htmlstate] => on_nl2br [postusername] => Dan [ip] => ddjones@riddlem [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 4 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> $_ =~ /^\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 Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <86sm8iy1w4.fsf@blue.stonehenge.com> [ref] => <27349158$1097671073416d21a133b5c1.57540341@config2.schlund.de> [htmlstate] => on_nl2br [postusername] => Randal [ip] => merlyn@stonehen [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 6 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) -->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: org ||> For additional commands, e-mail: org ||> ||> ||> ||> || || || ||-- ||To unsubscribe, e-mail: org ||For additional commands, e-mail: org || [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <000301c4b14e$ade9c280$060aa8c0@WinXPY2004> [ref] => <3505.209.167.16.15.1097681031.squirrel@209.167.16.15> [htmlstate] => on_nl2br [postusername] => Babale [ip] => perl@fongo.de [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 7 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> /^\w\w*-?\w+?(\.\w\w*-?\w+?)*$/ Now it won't match "host-.domain.com" like names. Other thing is this will allow underscore also in dns names.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 [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => [ref] => <000301c4b14e$ade9c280$060aa8c0@WinXPY2004> [htmlstate] => on_nl2br [postusername] => K.Prabakar [ip] => kprabakar@nmswo [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 10 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> /^\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 [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => [ref] => <000301c4b14e$ade9c280$060aa8c0@WinXPY2004> [htmlstate] => on_nl2br [postusername] => K.Prabakar [ip] => kprabakar@nmswo [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 11 [islastshown] => 1 [isfirstshown] => [attachments] => [allattachments] => ) --> Regex to match valid host or dns names - PERL Beginners

Regex to match valid host or dns names - PERL Beginners

Hi, How do I best test for a valid host name? My script will read from a file which contains IP addresses and/or dns name, so I'm trying to sort out valid IP addresses or host name (using regex). For some reason, my regex fails to match host names with one or more dash (-). IN the 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, $_ ...

  1. #1

    Default Regex to match valid host or dns names


    Hi,

    How do I best test for a valid host name? My script will read from a
    file which contains IP addresses and/or dns name, so I'm trying to sort
    out valid IP addresses or host name (using regex). For some reason, my
    regex fails to match host names with one or more dash (-). IN the
    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+$/;
    }

    Thanks..


    Babs
    perl@fongo.de Guest

  2. #2

    Default Re: =?iso-8859-1?Q?Regex_to_match_valid_host_or_dns_names?=

     




    /^\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
    K.Prabakar Guest

  3. #3

    Default Re: Regex to match valid host or dns names

    > [/ref]

    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



     


    Steve Guest

  4. #4

    Default Re: Regex to match valid host or dns names

    On Wed, 2004-10-13 at 15:06, K.Prabakar wrote: 
    >
    >
    >
    >
    > /^\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.[/ref]

    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, $_)
    }
    }



    Dan Guest

  5. #5

    Default Re: Regex to match valid host or dns names

    >>>>> "Steve" == Steve Bertrand <ca> 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
    <com> <URL: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!
    Randal Guest

  6. #6

    Default Re: Regex to match valid host or dns names

    >>>>> "K" == K Prabakar <co.in> 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
    <com> <URL: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!
    Randal Guest

  7. #7

    Default RE: Regex to match valid host or dns names


    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:ca]
    ||Sent: Wednesday, October 13, 2004 5:24 PM
    ||To: K.Prabakar
    ||Cc: de; org
    ||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: org
    ||> For additional commands, e-mail: org
    ||> <http://learn.perl.org/> <http://learn.perl.org/first-response>
    ||>
    ||>
    ||>
    ||
    ||
    ||
    ||--
    ||To unsubscribe, e-mail: org
    ||For additional commands, e-mail: org
    ||<http://learn.perl.org/> <http://learn.perl.org/first-response>



    Babale Guest

  8. #8

    Default RE: Regex to match valid host or dns names

     

    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.

    <snip>

    http://danconia.org
    Wiggins Guest

  9. #9

    Default Re: Regex to match valid host or dns names

    de wrote: 

    Hello,
     

    If you don't mind using a DNS lookup which will probably be slower than using
    a regular expression:

    use Socket;

    my hosts = qw(192.168.22.1 192.168.22.18 localhost another.host.domain
    host-no.top-level my.host.domain.com);

    my ( ips, dns );
    for my $host ( hosts ) {
    defined( my $ip = inet_aton( $host ) ) or do { warn "$host not valid.\n";
    next };
    push { $host eq inet_ntoa( $ip ) ? \ips : \dns }, $host;
    }



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  10. #10

    Default RE: Regex to match valid host or dns names

    On Wed, 13 Oct 2004, Babale Fongo wrote:
     


    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 underscore also in dns names.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
    K.Prabakar Guest

  11. #11

    Default RE: Regex to match valid host or dns names

    On Thu, 14 Oct 2004, K.Prabakar wrote:
     [/ref]

    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
    K.Prabakar Guest

Similar Threads

  1. regex to NOT match?
    By Ruby in forum Ruby
    Replies: 15
    Last Post: January 11th, 09:13 PM
  2. Can a regex match numbers?
    By Jabez wilson in forum PERL Beginners
    Replies: 5
    Last Post: November 22nd, 11:15 PM
  3. Opposite of regex match?
    By Marv in forum PERL Miscellaneous
    Replies: 3
    Last Post: October 3rd, 09:31 PM
  4. RegEx to match names?
    By Patrick D. in forum PERL Miscellaneous
    Replies: 2
    Last Post: August 21st, 05:29 AM
  5. Regex to match ALL characters?
    By Linda Patterson in forum PHP Development
    Replies: 3
    Last Post: July 16th, 01:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •