Write a simple regular expression to match an IP address,
e-mail address, city-state-zipcode combination.
Answer Posted / santosh
The question is not properly framed. A regex is specific to
a language to some extent. Lets frame it more properly and
create some test cases:
1. Writing regex for Perl to identify an IP address.
2. IP address must be a "true" IP address.
3. It must not match anything more than an IP addresses.
4. Must pass the following tests:
a)0.0.0.0: Pass: This is a valid class A address, though a
reserved address. You may not see it often but nevertheless
it is a valid address. See
http://www.lincoln.edu/math/rmyrick/ComputerNetworks/InetRef
erence/26.htm
b)1.1.1.1: Pass
c)255.255.255.255:Pass
d)1.1.1.11111:Fail
e)256.1.1.1:Fail
f)256.256.256.256:Fail
g)-1.-1.-1.-1: Fail
h).... (four dots without any digits in between)
Another test can be presence of extra zeros, which can be
fine or not fine depending upon the OS. For example
01.01.01.01 should or should not be accepted? But we will
ignore it for the time being in our analysis.
Here is an analysis of the solutions given above as well as
my additional solutions:
1. /([0-255])(\.)$1\1$1\1$1/; : This works for egrep and
possibly vi and other *nix tools such as emacs. This does
*not* work for Perl because [0-255] will match
digits '0','1', '2' and '5' only. It will not match 63.
2. /((\d{1,3})(\.)){3}\d{1,3}/: Written for Perl as
obviously {} syntax is not available for many other
scripting languages. This does not check the semantics of
IP addresses. Fails tests d, e, f above.
3. (/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ && $1 <
255 && $2 < 255 && $3 < 255 && $4 < 255 ): Is almost
correct. Only fails d in above tests. However, it uses
logical expressions and is not a pure regular expression.
Checking for word boundaries will correct it. For example,
(/\b(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\b/ && $1 <
255 && $2 < 255 && $3 < 255 && $4 < 255 ) will pass d
above.
4. r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b': Passes all the
tests. This is the best answer.
5. /^([1-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]
\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25
[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/: Fails
test a. But only because it was meant to eliminate 0.0.0.0.
Otherwise this is okay, though a little verbose. #4 above
can be modified slightly to take care of 0.0.0.0 case and
is less verbose.
Hope this helps.
Regards,
Santosh
Is This Answer Correct ? | 7 Yes | 1 No |
Post New Answer View All Answers
How do I generate a list of all .html files in a directory?
Differentiate between arrays and list in perl.
How do you give functions private variables that retain their values between calls?
What is perl pop array function?
Mention how many ways you can express string in Perl?
In Perl, there are some arguments that are used frequently. What are that arguments and what do they mean?
What is the difference between use and require in perl?
Explain perl. When do you use perl for programming? What are the advantages of programming in perl?
How to code in perl to implement the tail function in unix?
What is the usage of -i and 0s options?
Which has highest precedence in between list and terms? Explain?
What is the syntax used in Perl grep function?
How to change a directory in perl?
Define perl scripting?
What is the difference between perl array and perl hash?