473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to Validate IPv4 address in Python using Regular Expression.

Expand|Select|Wrap|Line Numbers
  1. if(re.match("(^[2][0-5][0-5]|^[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})$",ip_addr)   != None):
  2.         print "Verified as passed"
  3. else:
  4.         print "Verified as failed"
Nov 30 '11 #1
3 17511
Hi..
Any idea how we can accept ip address in ipv4 format as input from user in python?
with input() function it does not accept more than one decimal input!!

thanks
Nov 30 '11 #2
bvdet
2,851 Expert Mod 2GB
Use raw_input() instead of input() to receive input from the user. I have taken abeesh ks's re pattern and added comments:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. ip_addr = "This is an example of an IPv4 address: 192.11.2.250"
  4.  
  5. patt = re.compile('''
  6.             ^                                       # Match start of string
  7.             ([2][0-5][0-5]|^[1]{0,1}[0-9]{1,2})     # Match decimal octet
  8.             \.                                      # Match '.' character
  9.             ([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})    # Match decimal octet
  10.             \.                                      # Match '.' character
  11.             ([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})    # Match decimal octet
  12.             \.                                      # Match '.' character
  13.             ([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})    # Match decimal octet
  14.             $                                       # Match end of string
  15.             ''', re.VERBOSE)
  16.  
  17. m = re.search(patt, ip_addr.split()[-1])
  18.  
  19. if m:
  20.     print "Valid IPv4 address"
  21. else:
  22.     print "Invalid IPv4 address"
  23.  
  24. print m.groups()
I have not verified that the re pattern is 100% valid, but it appears to work.
Nov 30 '11 #3
thanks abeesh/bvdet. I ll try out the code :)
Dec 2 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Alexander N. Spitzer | last post by:
If I have a machine with 3 virtual IP addresses (192.168.1.), how can I start 3 instances of the same RMI application (each started with different properties/configs), each listening on the port...
8
by: YAN | last post by:
Hi, I want to get the mac address from a machine, which i have the IP address of that machine, how can i do that? I know how to get the mac address of the local machine from the following code: ...
7
by: Privacy Advocate | last post by:
//crossposted to: comp.lang.javascript, alt.comp.lang.javascript in an effort to get factual answers from JavaScript experts// Simply put; Is it possible to obtain the real (actual) IP address of...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
4
by: andreas.w.h.k. :-\) | last post by:
How do I change the address location in the wsdl <wsdl:port name="SearchSoap12" binding="tns:SearchSoap12"> <soap12:address location="http://searchservices/engine/search.asmx" /> </wsdl:port> ...
1
by: Phoenix_ver10 | last post by:
I have a mailing list with multiple names going to the same addresses. I need one address with all the names for that address on it. I checked out the example on microsoft's site, but A: It doesn't...
1
by: Jamie J. Begin | last post by:
I'm very new to the world of Python and am trying to wrap my head around it's OOP model. Much of my OOP experience comes from VB.Net, which is very different. Let's say I wanted to create an...
6
by: Nicolas Noakes | last post by:
Hello, I would like to convert to following process to code. Any advice is welcome. I have a hardware device which requires the this procedure to set it's IP address. First create an static...
36
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an...
4
by: karen.homersham | last post by:
Just working through Apress Pro ASP NET.2.0.E Commerce in C Sharp. I am having problems compiling the following: using System; using System.Collections.Generic; using System.Text; namespace...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.