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

python中re.findall函數(shù)實(shí)例用法

 更新時間:2021年09月01日 08:33:51   作者:小妮淺淺  
在本篇文章里小編給大家整理了一篇關(guān)于python中re.findall函數(shù)實(shí)例用法相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。

1、findall函數(shù)返回字符串中所有匹配結(jié)果的正則表達(dá)式列表。

2、如果沒有分組的正則是返回的正則匹配,分組返回的是分組匹配而非整個正則匹配。

實(shí)例

找到所有與pattern匹配的子串(不重疊),并將其放入列表。

import re
lst = re.findall("[1-9]\d*","qw21313h1o58p4kjh8123jkh8435u")
for x in lst:
    print(x,end=" ")

#輸出結(jié)果:21313 1 58 4 8123 8435

實(shí)例擴(kuò)展:

python3中函數(shù)說明:

findall(pattern, string, flags=0)
    Return a list of all non-overlapping matches in the string.

    If one or more capturing groups are present in the pattern, return
    a list of groups; this will be a list of tuples if the pattern
    has more than one group.

    Empty matches are included in the result.

兩種形式的使用方法:

import re
kk = re.compile(r'\d+')
kk.findall('one1two2three3four4')
#[1,2,3,4]
 
#注意此處findall()的用法,可傳兩個參數(shù);
kk = re.compile(r'\d+')
re.findall(kk,"one123")
#[1,2,3]

其中,含()時要注意:

import re

string="abcdefg  acbdgef  abcdgfe  cadbgfe"

#帶括號與不帶括號的區(qū)別
#不帶括號
regex=re.compile("((\w+)\s+\w+)")
print(regex.findall(string))
#輸出:[('abcdefg  acbdgef', 'abcdefg'), ('abcdgfe  cadbgfe', 'abcdgfe')]

regex1=re.compile("(\w+)\s+\w+")
print(regex1.findall(string))
#輸出:['abcdefg', 'abcdgfe']

regex2=re.compile("\w+\s+\w+")
print(regex2.findall(string))
#輸出:['abcdefg  acbdgef', 'abcdgfe  cadbgfe']

到此這篇關(guān)于python中re.findall函數(shù)實(shí)例用法的文章就介紹到這了,更多相關(guān)python中re.findall函數(shù)的介紹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

舟山市| 宣武区| 宜宾县| 吉木乃县| 泰安市| 泽普县| 东源县| 调兵山市| 达日县| 南丰县| 卓尼县| 芦山县| 桐庐县| 南充市| 股票| 东乡县| 静乐县| 嘉禾县| 长泰县| 安化县| 麻江县| 通道| 苏尼特左旗| 无锡市| 永定县| 汝城县| 犍为县| 宾阳县| 达拉特旗| 湖州市| 聊城市| 陇西县| 洛南县| 台前县| 竹北市| 新竹市| 沙河市| 吴忠市| 泸西县| 会同县| 卫辉市|