最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python正則表達(dá)式中的量詞符號(hào)與組問題小結(jié)

 更新時(shí)間:2021年08月30日 11:42:57   作者:Insane_Loafer  
這篇文章主要介紹了Python正則表達(dá)式中的量詞符號(hào)與組問題小結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

正則表達(dá)式中的符號(hào)

在這里插入圖片描述

在這里插入圖片描述

例子

  •  | 是或的關(guān)系,只要存在就會(huì)被捕獲
  • 匹配到的數(shù)據(jù)只按字符串順序返回,而不是按照匹配規(guī)則返回
In [18]: data = 'insane@loafer.com'

In [19]: print(re.findall('insane|com|loafer', data))
['insane', 'loafer', 'com']

^ 等同于 \A

In [20]:  print(re.findall('^insane',data))
['insane']

In [21]:  print(re.findall('^insane1',data))
[]

$ 等同于 \Z

In [22]:  print(re.findall('com$',data))
['com']

In [23]:  print(re.findall('net$',data))
[]

* 匹配0次或多次

In [24]:  print(re.findall('\w*',data))
['insane', '', 'loafer', '', 'com', '']
  • + 匹配1次或多次
  • w+ 匹配1次或多次數(shù)字或字母
  • @.屬于0次范圍,不會(huì)被匹配出來
In [25]:  print(re.findall('\w+',data))
['insane', 'loafer', 'com']

{3} 表示對于匹配到的數(shù)據(jù)只獲取3次

In [31]: data = 'insane@loaf.com'

In [32]:  print(re.findall('\w{3}',data))
['ins', 'ane', 'loa', 'com']

In [33]:  print(re.findall('[a-z]{3}',data))
['ins', 'ane', 'loa', 'com']

[a-zA-Z0-9] 基本上等同于 \w

{M, N} 表示對于匹配到的數(shù)據(jù)只獲取M~N次

In [34]: data = 'insane@loaf.com'

In [35]:  print(re.findall('\w{1,4}',data))
['insa', 'ne', 'loaf', 'com']

反例:NM 中間不能有空格

In [36]:  print(re.findall('\w{1, 4}',data))
[]

[^...] 表示不匹配字符集中的字符

In [37]: data = 'insane@loaf.com'

In [38]:  print(re.findall('[^insane]',data))
['@', 'l', 'o', 'f', '.', 'c', 'o', 'm']

組的概念

在這里插入圖片描述

組的應(yīng)用

In [42]: test = 'hello my name is insane'

In [43]: result = re.search('hello (.*) name is (.*)', test)

In [44]: result.groups()
Out[44]: ('my', 'insane')

In [45]: result.groups(1)
Out[45]: ('my', 'insane')

In [46]: result.group(1)
Out[46]: 'my'

In [47]: result.group(2)
Out[47]: 'insane'
  • 貪婪與非貪婪 0次或多次屬于貪婪模式
  • 通過?組合變成非貪婪模式 實(shí)戰(zhàn)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time     : 2021/8/28 22:13
# @Author   : InsaneLoafer
# @File     : re_test2.py

import re


def check_url(url):
    """
    判斷url是否合法
    :param url:
    :return:
    """
    result = re.findall('[a-zA-Z]{4,5}://\w*\.*\w+\.\w+', url)
    if len(result) != 0:
        return True
    else:
        return False

def get_url(url):
    """
    通過組獲取url中的某一部分
    :param url:
    :return:
    """
    result = re.findall('[https://|http://](\w*\.*\w+\.\w+)', url)
    if len(result) != 0:
        return result[0]
    else:
        return ''

def get_email(data):
    # result = re.findall('[0-9a-zA-Z_]+@[0-9a-zA-Z]+\.[a-zA-Z]+', data)
    result = re.findall('.+@.+\.[a-zA-Z]+', data)
    return result


html = ('<div class="s-top-nav" style="display:none;">'
        '</div><div class="s-center-box"></div>')

def get_html_data(data):
    """
    獲取style中的display:
    使用非貪婪模式
    """
    result = re.findall('style="(.*?)"', data)
    return result

def get_all_data_html(data):
    """
    獲取html中所有等號(hào)后雙引號(hào)內(nèi)的字符
    :param data:
    :return:
    """
    result = re.findall('="(.+?)"', data)
    return result


if __name__ == '__main__':
    result = check_url('https://www.baidu.com')
    print(result)

    result = get_url('https://www.baidu.com')
    print(result, 'https')

    result = get_url('http://www.baidu.com')
    print(result, 'http')

    result = get_email('insane@163.net')
    print(result)

    result = get_html_data(html)
    print(result)

    result = get_all_data_html(html)
    print(result)
True
www.baidu.com https
www.baidu.com http
['insane@163.net']
['display:none;']
['s-top-nav', 'display:none;', 's-center-box']

Process finished with exit code 0

到此這篇關(guān)于Python正則表達(dá)式中的量詞符號(hào)與組的文章就介紹到這了,更多相關(guān)python正則表達(dá)式量詞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于Python下載大文件時(shí)哪種方式速度更快

    關(guān)于Python下載大文件時(shí)哪種方式速度更快

    這篇文章主要介紹了關(guān)于Python下載大文件時(shí)哪種方式速度更快,通常,我們都會(huì)用 requests 庫去下載,這個(gè)庫用起來太方便了,需要的朋友可以參考下
    2023-04-04
  • Python實(shí)現(xiàn)希爾排序算法的原理與用法實(shí)例分析

    Python實(shí)現(xiàn)希爾排序算法的原理與用法實(shí)例分析

    這篇文章主要介紹了Python實(shí)現(xiàn)希爾排序算法,簡單講述了希爾排序的原理并結(jié)合具體實(shí)例形式分析了Python希爾排序的具體實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下
    2017-11-11
  • python中的plt.cm.Paired用法說明

    python中的plt.cm.Paired用法說明

    這篇文章主要介紹了python中plt.cm.Paired的用法說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 使用Pyinstaller打包exe文件詳細(xì)圖文教程

    使用Pyinstaller打包exe文件詳細(xì)圖文教程

    PyInstaller可以用來打包python應(yīng)用程序,打包完的程序就可以在沒有安裝Python解釋器的機(jī)器上運(yùn)行了,下面這篇文章主要給大家介紹了關(guān)于使用Pyinstaller打包exe文件的詳細(xì)圖文教程,需要的朋友可以參考下
    2022-08-08
  • pytest配置項(xiàng)目不同環(huán)境URL的實(shí)現(xiàn)

    pytest配置項(xiàng)目不同環(huán)境URL的實(shí)現(xiàn)

    pytest-base-url是pytest的第三方插件,主要用來幫助我們進(jìn)行切換測試環(huán)境地址,下面就來介紹一下配置不同環(huán)境URL的實(shí)現(xiàn),感興趣的可以了解一下
    2024-02-02
  • Python驗(yàn)證文件是否可讀寫代碼分享

    Python驗(yàn)證文件是否可讀寫代碼分享

    這篇文章主要介紹了Python驗(yàn)證文件是否可讀寫代碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • 出現(xiàn)module 'queue' has no attribute 'Queue'問題的解決

    出現(xiàn)module 'queue' has no attrib

    這篇文章主要介紹了出現(xiàn)module 'queue' has no attribute 'Queue'問題的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • python3將變量寫入SQL語句的實(shí)現(xiàn)方式

    python3將變量寫入SQL語句的實(shí)現(xiàn)方式

    這篇文章主要介紹了python3將變量寫入SQL語句的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python自動(dòng)化unittest yaml使用過程解析

    python自動(dòng)化unittest yaml使用過程解析

    這篇文章主要介紹了python自動(dòng)化unittest yaml使用過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • python學(xué)生信息管理系統(tǒng)實(shí)現(xiàn)代碼

    python學(xué)生信息管理系統(tǒng)實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了python學(xué)生信息管理系統(tǒng)的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06

最新評論

华容县| 威信县| 德安县| 临高县| 大厂| 开原市| 行唐县| 苍南县| 岐山县| 紫金县| 定襄县| 周口市| 黔东| 阜阳市| 英山县| 乌鲁木齐市| 北辰区| 平阴县| 惠来县| 八宿县| 通州区| 二连浩特市| 凤阳县| 张家港市| 高州市| 商河县| 登封市| 神农架林区| 乌恰县| 岐山县| 泽普县| 安阳市| 柞水县| 靖西县| 顺平县| 孟津县| 渝北区| 汉中市| 准格尔旗| 黔江区| 威远县|