文章詳情頁
關于python正則表達式中函數(shù)findall的用法?
瀏覽:102日期:2022-09-20 17:47:56
問題描述
1.我的目標是匹配一個地址 ex:123 xuancheng anhui China2.我給的正則表達式為:r’d+(s+[a-zA-Z]+)+s*’代碼如下:
pattern = r’d+(s+[a-zA-Z]+)+s*’basestr = ’123 xuancheng china ’m1 = re.findall(pattern, basestr)if m1 is not None: print m1得到的結果為 [’ china’]
這個是以圓括號的內容進行匹配的,我想要配置的是整句話,如何改造呢?除了以下這個用法pattern_2 = r’(d+(s+[a-zA-Z]+)+s*)’返回 [(’123 xuancheng china ’, ’ china’)]使用search 函數(shù)能得到想要的結果m2 = re.search(pattern, basestr)print m2.group()得到:123 xuancheng china
問題解答
回答1:r’d+(?:s+[a-zA-Z]+)+s*’
回答2:簡單粗暴是這樣的,請自己再修改。
再修改一下是這樣的
提供的新的字符串也可以。
相關文章:
1. 對mysql某個字段監(jiān)控的功能2. showpassword里的this 是什么意思?代表哪個元素3. html - vue項目中用到了elementUI問題4. JavaScript事件5. python - 為什么正常輸出中文沒有亂碼,zip函數(shù)之后出現(xiàn)中文編程unicode編碼的問題,我是遍歷輸出的啊。6. python - 使用readlines()方法讀取文件內容后,再用for循環(huán)遍歷文件與變量匹配時出現(xiàn)疑難?7. javascript - table列過多,有什么插件可以提供列排序和選擇顯示列的功能8. javascript - js中向下取整9. javascript - HTML字符串排版10. html5 - ElementUI table中el-table-column怎么設置百分比顯示。
排行榜

熱門標簽