Table of Contents
ip138.com IP查询(搜索IP地址的地理位置)
您查询的IP:121.0.29.231
Home Backend Development Python Tutorial python中正则表达式的使用详解

python中正则表达式的使用详解

Jun 06, 2016 am 11:19 AM
python regular expression

从学习Python至今,发现很多时候是将Python作为一种工具。特别在文本处理方面,使用起来更是游刃有余。

说到文本处理,那么正则表达式必然是一个绝好的工具,它能将一些繁杂的字符搜索或者替换以非常简洁的方式完成。

我们在处理文本的时候,或是查询抓取,或是替换.

一.查找
如果你想自己实现这样的功能模块,输入某一个ip地址,得到这个ip地址所在地区的详细信息.

然后你发现http://ip138.com 可以查出很详细的数据

但是人家没有提供api供外部调用,但是我们可以通过代码模拟查询然后对结果进行抓取.

通过查看这个相应页面的源码,我们可以发现,结果是放在三个

  • 中的

     

    代码如下:


     
         
             
         
         
             
         
         
     
             
         
         
             
     
         
         
         
             
       
     

     
         

    ip138.com IP查询(搜索IP地址的地理位置)

    您查询的IP:121.0.29.231

    • 本站主数据:浙江省杭州市 阿里巴巴
    • 参考数据一:浙江省杭州市 阿里巴巴
    • 参考数据二:浙江省杭州市 阿里巴巴
    如果您发现查询结果不详细或不正确,请使用IP数据库自助添加功能进行修正

     
           

    IP地址或者域名:
       

     

    如果你了解正则表达式你可能会写出

    正则表达式

     

    代码如下:


    (?).*?(?=)

     

    这里使用了前瞻:lookahead 后顾: lookbehind,这样的好处就是匹配的结果中就不会包含html的li标签了.

    如果你对自己写的正则表达式不是很自信的话,可以在一些在线或者本地的正则测试工具进行一些测试,以确保正确.

    接下来的工作就是如果用Python实现这样的功能,首先我们得将正则表达式表示出来:

     

    代码如下:


    r"(?).*?(?=)" 

     

     Python中字符串前面加上前导r这个字符,代表这个字符串是R aw String(原始字符串),也就是说Python字符串本身不会对字符串中的字符进行转义.这是因为正则表达式也有转义字符之说,如果双重转义的话,易读性很差.

    这样的串在Python中我们把它叫做"regular expression pattern"

    如果我们对pattern进行编译的话

     

    代码如下:


    prog = re.compile(r"(?).*?(?=)") 

     

    我们便可以得到一个正则表达式对象regular expression object,通过这个对象我们可以进行相关操作.

    比如

     

    代码如下:


    result=prog.match(string) 
    ##这个等同于 
    result=re.match(r"(?).*?(?=)",string) 
    ##但是如果这个正则需要在程序匹配多次,那么通过正则表达式对象的方式效率会更高 

     

    接下来就是查找了,假设我们的html结果已经以html的格式存放在text中,那么通过

     

    代码如下:


    result_list = re.findall(r"(?).*?(?=)",text) 

     

    便可以取得所需的结果列表.

    二.替换
    使用正则表达式进行替换非常的灵活.

    比如之前我在阅读Trac这个系统中wiki模块的源代码的时候,就发现其wiki语法的实现就是通过正则替换进行的.

    在使用替换的时候会涉及到正则表达式中的Group分组的概念.

    假设wiki语法中使用!表示转义字符即感叹号后面的功能性字符会原样输出,粗体的语法为

    写道
    '''这里显示为粗体'''
     那么有正则表达式为

     

    代码如下:


    r"(?P!?''')" 

     

      这里的?P是Python正则语法中的一部分,表示其后的group的名字为"bold"

      下面是替换时的情景,其中sub函数的第一个参数是pattern,第二个参数可以是字符串也可以是函数,如果是字符串的话,那么就是将目标匹配的结果替换成指定的结果,而如果是函数,那么函数会接受一个match object的参数,并返回替换后的字符串,第三个参数便是源字符串.

     

    代码如下:


    result = re.sub(r"(?P!?''')", replace, line) 

     

    每当匹配到一个三单引号,replace函数便运行一次,可能这时候需要一个全局变量记录当前的三单引号是开还是闭,以便添加相应的标记.

    在实际的trac wiki的实现的时候,便是这样通过一些标记变量,来记录某些语法标记的开闭,以决定replace函数的运行结果.

    --------------------

    示例

    一. 判断字符串是否是全部小写

    代码

     

    代码如下:


    # -*- coding: cp936 -*-
    import re 
    s1 = 'adkkdk'
    s2 = 'abc123efg'

     

    an = re.search('^[a-z]+$', s1)
    if an:
        print 's1:', an.group(), '全为小写'
    else:
        print s1, "不全是小写!"

    an = re.match('[a-z]+$', s2)
    if an:
        print 's2:', an.group(), '全为小写'
    else:
        print s2, "不全是小写!"

     

    结果

     

    究其因

    1. 正则表达式不是python的一部分,利用时需要引用re模块

    2. 匹配的形式为: re.search(正则表达式, 带匹配字串)或re.match(正则表达式, 带匹配字串)。两者区别在于后者默认以开始符(^)开始。因此,

    re.search('^[a-z]+$', s1) 等价于 re.match('[a-z]+$', s2)
    3. 如果匹配失败,则an = re.search('^[a-z]+$', s1)返回None

    group用于把匹配结果分组

    例如

     

    代码如下:


    import re
    a = "123abc456"
    print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(0)   #123abc456,返回整体
    print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(1)   #123
    print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(2)   #abc
    print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(3)   #456

     

    1)正则表达式中的三组括号把匹配结果分成三组

      group() 同group(0)就是匹配正则表达式整体结果

      group(1) 列出第一个括号匹配部分,group(2) 列出第二个括号匹配部分,group(3) 列出第三个括号匹配部分。

    2)没有匹配成功的,re.search()返回None

    3)当然郑则表达式中没有括号,group(1)肯定不对了。

    二.  首字母缩写词扩充

    具体示例

    FEMA   Federal Emergency Management Agency
    IRA    Irish Republican Army
    DUP    Democratic Unionist Party

    FDA    Food and Drug Administration
    OLC    Office of Legal Counsel
    分析

    缩写词  FEMA
    分解为  F*** E*** M*** A***
    规律    大写字母 + 小写(大于等于1个)+ 空格
    参考代码

     

    代码如下:


    import re
    def expand_abbr(sen, abbr):
        lenabbr = len(abbr)
        ma = ''
        for i in range(0, lenabbr):
            ma += abbr[i] + "[a-z]+" + ' '
        print 'ma:', ma
        ma = ma.strip(' ')
        p = re.search(ma, sen)
        if p:
            return p.group()
        else:
            return ''

     

    print expand_abbr("Welcome to Algriculture Bank China", 'ABC')

     

    结果

    问题

    上面代码对于例子中的前3个是正确的,但是后面的两个就错了,因为大写字母开头的词语之间还夹杂着小写字母词

    规律

    大写字母 + 小写(大于等于1个)+ 空格 + [小写+空格](0次或1次)

    参考代码

     

    代码如下:


    import re
    def expand_abbr(sen, abbr):
        lenabbr = len(abbr)
        ma = ''
        for i in range(0, lenabbr-1):
            ma += abbr[i] + "[a-z]+" + ' ' + '([a-z]+ )?'
        ma += abbr[lenabbr-1] + "[a-z]+"
        print 'ma:', ma
        ma = ma.strip(' ')
        p = re.search(ma, sen)
        if p:
            return p.group()
        else:
            return ''

     

    print expand_abbr("Welcome to Algriculture Bank of China", 'ABC')

     

    技巧

    中间的 小写字母集合+一个空格,看成一个整体,就加个括号。要么同时有,要么同时没有,这样需要用到?,匹配前方的整体。

    三. 去掉数字中的逗号

    具体示例

    在处理自然语言时123,000,000如果以标点符号分割,就会出现问题,好好的一个数字就被逗号肢解了,因此可以先下手把数字处理干净(逗号去掉)。

    分析

    数字中经常是3个数字一组,之后跟一个逗号,因此规律为:***,***,***

    正则式

    [a-z]+,[a-z]?

    参考代码3-1

     

    代码如下:


    import re

     

    sen = "abc,123,456,789,mnp"
    p = re.compile("\d+,\d+?")

    for com in p.finditer(sen):
        mm = com.group()
        print "hi:", mm
        print "sen_before:", sen
        sen = sen.replace(mm, mm.replace(",", ""))
        print "sen_back:", sen, '\n'

     

    结果

    技巧

    使用函数finditer(string[, pos[, endpos]]) | re.finditer(pattern, string[, flags]):

    搜索string,返回一个顺序访问每一个匹配结果(Match对象)的迭代器。     

    参考代码3-2

     

    代码如下:


    sen = "abc,123,456,789,mnp"
    while 1:
        mm = re.search("\d,\d", sen)
        if mm:
            mm = mm.group()
            sen = sen.replace(mm, mm.replace(",", ""))
            print sen
        else:
            break

     

    结果

    延伸

    这样的程序针对具体问题,即数字3位一组,如果数字混杂与字母间,干掉数字间的逗号,即把“abc,123,4,789,mnp”转化为“abc,1234789,mnp”

    思路

    更具体的是找正则式“数字,数字”找到后用去掉逗号的替换

    参考代码3-3

     

    代码如下:


    sen = "abc,123,4,789,mnp"
    while 1:
        mm = re.search("\d,\d", sen)
        if mm:
            mm = mm.group()
            sen = sen.replace(mm, mm.replace(",", ""))
            print sen
        else:
            break
    print sen

     

    结果

    四. 中文处理之年份转换(例如:一九四九年--->1949年)

    中文处理涉及到编码问题。例如下边的程序识别年份(****年)时

     

    代码如下:


    # -*- coding: cp936 -*-
    import re
    m0 =  "在一九四九年新中国成立"
    m1 =  "比一九九零年低百分之五点二"
    m2 =  '人一九九六年击败俄军,取得实质独立'

     

    def fuc(m):
        a = re.findall("[零|一|二|三|四|五|六|七|八|九]+年", m)
        if a:
            for key in a:
                print key
        else:
            print "NULL"

    fuc(m0)
    fuc(m1)
    fuc(m2)

     

    运行结果

    可以看出第二个、第三个都出现了错误。

    改进——准化成unicode识别

     

    代码如下:


    # -*- coding: cp936 -*-
    import re
    m0 =  "在一九四九年新中国成立"
    m1 =  "比一九九零年低百分之五点二"
    m2 = '人一九九六年击败俄军,取得实质独立'

     

    def fuc(m):
        m = m.decode('cp936')
        a = re.findall(u"[\u96f6|\u4e00|\u4e8c|\u4e09|\u56db|\u4e94|\u516d|\u4e03|\u516b|\u4e5d]+\u5e74", m)

        if a:
            for key in a:
                print key
        else:
            print "NULL"

    fuc(m0)
    fuc(m1)
    fuc(m2)

     

    结果

    识别出来可以通过替换方式,把汉字替换成数字。

    参考

     

    代码如下:


    numHash = {}
    numHash['零'.decode('utf-8')] = '0'
    numHash['一'.decode('utf-8')] = '1'
    numHash['二'.decode('utf-8')] = '2'
    numHash['三'.decode('utf-8')] = '3'
    numHash['四'.decode('utf-8')] = '4'
    numHash['五'.decode('utf-8')] = '5'
    numHash['六'.decode('utf-8')] = '6'
    numHash['七'.decode('utf-8')] = '7'
    numHash['八'.decode('utf-8')] = '8'
    numHash['九'.decode('utf-8')] = '9'

     

    def change2num(words):
        print "words:",words
        newword = ''
        for key in words:
            print key
            if key in numHash:
                newword += numHash[key]
            else:
                newword += key
        return newword

    def Chi2Num(line):
        a = re.findall(u"[\u96f6|\u4e00|\u4e8c|\u4e09|\u56db|\u4e94|\u516d|\u4e03|\u516b|\u4e5d]+\u5e74", line)
        if a:
            print "------"
            print line
            for words in a:
                newwords = change2num(words)
                print words
                print newwords
                line = line.replace(words, newwords)
        return line

     

  • Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

    Google AI has started to provide developers with access to extended context windows and cost-saving features, starting with the Gemini 1.5 Pro large language model (LLM). Previously available through a waitlist, the full 2 million token context windo

    How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

    How to download DeepSeek Xiaomi? Search for "DeepSeek" in the Xiaomi App Store. If it is not found, continue to step 2. Identify your needs (search files, data analysis), and find the corresponding tools (such as file managers, data analysis software) that include DeepSeek functions.

    How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

    The key to using DeepSeek effectively is to ask questions clearly: express the questions directly and specifically. Provide specific details and background information. For complex inquiries, multiple angles and refute opinions are included. Focus on specific aspects, such as performance bottlenecks in code. Keep a critical thinking about the answers you get and make judgments based on your expertise.

    How to search deepseek How to search deepseek Feb 19, 2025 pm 05:18 PM

    Just use the search function that comes with DeepSeek. Its powerful semantic analysis algorithm can accurately understand the search intention and provide relevant information. However, for searches that are unpopular, latest information or problems that need to be considered, it is necessary to adjust keywords or use more specific descriptions, combine them with other real-time information sources, and understand that DeepSeek is just a tool that requires active, clear and refined search strategies.

    How to program deepseek How to program deepseek Feb 19, 2025 pm 05:36 PM

    DeepSeek is not a programming language, but a deep search concept. Implementing DeepSeek requires selection based on existing languages. For different application scenarios, it is necessary to choose the appropriate language and algorithms, and combine machine learning technology. Code quality, maintainability, and testing are crucial. Only by choosing the right programming language, algorithms and tools according to your needs and writing high-quality code can DeepSeek be successfully implemented.

    How to use deepseek to settle accounts How to use deepseek to settle accounts Feb 19, 2025 pm 04:36 PM

    Question: Is DeepSeek available for accounting? Answer: No, it is a data mining and analysis tool that can be used to analyze financial data, but it does not have the accounting record and report generation functions of accounting software. Using DeepSeek to analyze financial data requires writing code to process data with knowledge of data structures, algorithms, and DeepSeek APIs to consider potential problems (e.g. programming knowledge, learning curves, data quality)

    The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

    Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

    Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

    Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

    See all articles