Python Regular Expressions Match ip address instances and python Regular Expressions

Source: Internet
Author: User

Python Regular Expressions Match ip address instances and python Regular Expressions

This example describes an instance where the regular expression matches an IP address. The code structure is easy to understand. Share it with you for your reference.

The main implementation code is as follows:

import rereip = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')  for ip in reip.findall(line):    print "ip>>>", ip

I hope this article will help you with Python programming.


A python Regular Expression

#! /Usr/bin/env python3
# Coding = UTF-8

"""
Parse ip and path
"""

Import re

Def checkpath (s ):
Express = r "(\ d + \. \ d +) :(. + )"
Mo = re. search (express, s)
If mo:
Ip = mo. group (1)
Path = mo. group (2)
Print ('IP is % s, path is % s' % (IP, path ))
Return ip, path
Else:
Print ('the path you input is invalid ')
Return None

Def test_checkpath ():
Assert checkpath ('192. 168.22.77:/home/adf ') = ('192. 168.22.77 ',
'/Home/fund ')
Assert checkpath ('192. 192:/home/jd') is None
Assert checkpath ('192. 168.22.77/home/fund') is None
Assert checkpath ('192. 168.22.77: ') is None

Pay attention to the usage of re module. This is only a preliminary judgment. If you really want to determine the validity of the IP address, you cannot use it alone. For example, if each group of numbers must be <255, no judgment is made. You can adjust whether or not null values are allowed in the path section. The general usage is for reference.

If you do not need to consider the validity of the input, directly
Ip, path = input_string.split (':')
That's all.

How does the python regular expression match strings?

Complete code :#! /Usr/bin/python #-*-coding: UTF-8 -*-
"""
Function:
How does the python regular expression match strings?

Author: Crifan LiVersion: 2012-12-08
Contact: admin at crifan dot com
"""

Import re;
Txt = "bn ', 'dd _ ff'); xm_a ([1, 'zhangming', 0.999, 'yuwenjige', 'lishijige, 0.999,]); xm_a ([2, 'wangmeng', 'shuxuejige ', 'dilijige', 0,]); xm_a ([3, 'wangli ', 0.999, 'shuxuejige', 'dilijige ',]); zuobi (6, 3, '4: 5 '); fg_gh ('xxx', 'vb _ 4'); xm_a ([4, 'dash', 'huaxuejige', 'yingyujige, 1, 0.999,]); </";
# Note: Forward-Negative match is used here # If you are not familiar with it, refer:
# [Tutorial] Explain the Python Regular Expression in detail :(?!...) Negative lookahead assertion forward negative match/forward negative assertions
Jige = re. findall ("xm_a \ (\ [\ d +, '(\ w +)', [^ \ (\) \ [\] + \] \); (?! Zuobi) ", txt );
Print jige; # ['zhangming', 'wangmeng', 'dash']

In addition, if you are interested, you can go to my series of tutorials:
[Tutorial] describes Python Regular Expressions

(No post address is provided here. Please search for the title on google to find the post address)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.