Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Need a Regular expression that evaluates a host name and ip address->

Last post 11-20-2008, 5:54 PM by Aussie Susan. 5 replies.
Sort Posts: Previous Next
  •  11-18-2008, 6:25 AM 48438

    Need a Regular expression that evaluates a host name and ip address->

    Hi folks

    I am using java as an environment  for my project. i am using a source file server field that uses both the ip address and host name. this project will be deployed in linux evt.

     i build a regex in the following way 

    ^((([0-9]{1,2})|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\.){3}(([0-9]{1,2})|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))$) || ((^[a-zA-Z0-9-])(([a-zA-Z0-9]*)[a-zA-Z0-9]$)

     

    but actually wat is the requirement is the host name should allow atleast one alphabet but i am not able to get a regex for that can anybody of you sugest a solution for that

    the first half of regex is used to validate ip address(works proper) but the second half is not working proper if i give 57 as input it takes it as a valid host name can anybody of you give me a solution

  •  11-18-2008, 7:40 PM 48480 in reply to 48438

    Re: Need a Regular expression that evaluates a host name and ip address->

    I'm assuming the following:

    - the "\\.' subpattern is there because you need to escape the backslash to get the regex engine to see the literal '.' character
    - if the value is an IP address then it gets captured in one match group, and if it is a name then it gets captured in another
    - as posted the pattern has unbalanced parentheses (an extra ')' in the first part and a missing ')' in the second - I would have expected you to be getting syntax errors when you ran this so I'm assuming this is a transcription error

    I have also removed some of the unnecessary parentheses so as to make it clearer what you are trying to do, and I think you are close to the answer. The following works for me:

    (^(([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$)|(^[a-zA-Z0-9-][a-zA-Z0-9]*[a-zA-Z0-9]$)

    Now for the problems you mentioned. As written, the host name part must be at least 2 characters long and the only difference is that the firtst character can be a dash as well as the upper or lower case letter or a digit. You really need to tell us exactly what your rules are for a valid name so that we can help you with this, but if they are that the first letter must be a alphabet character and this can be followed by any number of alphanumeric or '-' characters, then

    [a-zA-Z]([a-zA-Z0-9-]*)

    would do (assuming that you are not wanting to use the 'ignore case' regex option ).

    As written, you "host name' of 57 would match because you allow the first character to be a digit, it will skip the option middle characters and the last character can be a digit as well. Therefore 57 is considered to be perfectly valid.

    You may well be having a problem with the alternation between the two parts of this pattern (the IP address and the host name) because you have used two alternation characters in a row. In effect this will allow a null string to match the pattern as a whole.

    Can I suggest that you create a series of test cases that include invalid values as well as valid ones to test against. Also test each part of your pattern separately at first and make sure that they are performing as expected before joining them and re-running the entire set of tests.

    By the way, your IP address subpattern allows the first value to be 0 which I don't think is valid. A small point but you have gone to some lengths to validate the numbers as well so it looks like you are not only checking that they are all digits but that the digits form valid address range values - it seems a shame to miss out on an important one.

    Susan

  •  11-19-2008, 1:47 AM 48490 in reply to 48480

    Re: Need a Regular expression that evaluates a host name and ip address->

    Hi susan

    Thanks for your reply. actually, requirement for host name validation is mentioned below

    1). It can contain only dots, dash and alphanumeric characters. 2). It cannot be more than 63 characters in length(which i validated thru java). 3). The first character must be an alphanumeric or dash. 4). The last character cannot be a dot or dash. 5). There should atleast be one alphabet in the host name.

     but the proposed solution does not solve the criteria as it is not necessary that the alphabet must be at the 1st position, it can be present at any part of the string so can you suggest me some other solution

    Note : The ip address sub pattern is working 100% fine except the zero value that you mention in your post i have changed it as

    (^(([1-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$)|(^[a-zA-Z0-9-][a-zA-Z0-9]*[a-zA-Z0-9]$)

    but the second part for host name that is  (^[a-zA-Z0-9-][a-zA-Z0-9]*[a-zA-Z0-9]$) is not working as required so can you give me a solution as per the requirement that i have mentioned above

  •  11-19-2008, 6:09 PM 48534 in reply to 48490

    Re: Need a Regular expression that evaluates a host name and ip address->

    I think you have taken my comment about not allowing 0 in the first position a bit too far - your pattern will not allow 10.0.0.1 which is perfectly valid.

    I think the following (split over several lines to make the exaplanation easier but you will need the pattern as a single line) does all you want:

    ^([1-9]|1\d{1,2}|2[0-4]\d|25[0-5])
      (\.(\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])){3}$
    |
    ^(?=.*?[a-z])(?!\.)[a-z\d.-]*[a-z\d]$

    (I've assumed the 'ignore case' option is set - you can adjust the character class definitions is not)

    The first line matches the first IP address value in the range from 1 to 255. The second line matches the remaining 3 IP address components, each with a leading '.' and the values from 0 to 255.

    The 3rd line is the main alternation between the IP address and host name parts

    The 4th line checks you rules (except #2 which you say you check elsewhere). The first part makes sure that there is at least 1 alpha somewhere in the text (rule 5). The second lookahead makes sure that the first character in the text is not a dot (intersection of rule 1 and rule 3). Next follows a match for 0 or more alphanumerics, dots and dashes (rule 1) and the last part makes sure that the last character is alphanumeric (intersection of rule 1 and rule 4).

    I've tested this with the following test cases - the first set all match and none of the second set do:

    10.0.0.1
    127.0.0.1
    192.168.0.1
    host-name
    host.name...fred
    -host.name
    -host69
    a
    47-answer

    0.10.10.2
    .host-name
    host%name
    host.
    host-
    -
    42

     

    Susan

  •  11-20-2008, 2:58 AM 48555 in reply to 48534

    Re: Need a Regular expression that evaluates a host name and ip address->

    Thanks a lot susan it is working as required...

    I have still one more issue it is validation rules.. as per the linux platform i have given the validation of host name or DNS name  in my previous post, is there any globalized validation rules for host name or DNS name do you have any idea about it

     

    regards

    shiva

  •  11-20-2008, 5:54 PM 48588 in reply to 48555

    Re: Need a Regular expression that evaluates a host name and ip address->

    I'm sorry but I can't help you with that as my knowledge of such things is very limited. That's one of the reasons why I (at least) need to have the 'rules' fully explained to me so that I can create the regex based on those.

    However, ther are others in this forum who have a much broader understanding than I do who may be able to help.

    Susan

View as RSS news feed in XML