日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

正则表达式中(?:)的巨大作用

發布時間:2025/3/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 正则表达式中(?:)的巨大作用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

理論上的解釋:

1、其中?:的作用是匹配Swift3模式字符串,但不會存儲該匹配。

2、It's called a 'non-capturing group', which means the regex would?not?make a group by the match inside the parenteses like it would otherwise do (normally, a parenthesis creates a group)

3、When you use?(?:)?it means that the?group?is matched but is not captured for back-referencing i.e?non-capturing?group. It's not stored in memory to be referenced later on.

實際的效果:

import re y= '123@qq.comaaa@163.combbb@126.comasdfasfs33333@adfcom' #第一種寫法: ret1=re.findall('[0-9a-z]+@+[0-9a-z]+\.com',y) print(ret1) ##第二種寫法: ret2=re.findall('(\w+@(qq|126|163)\.com)',y) #for i in ret2: # print(i[0]) print(ret2) #第三種寫法: ret3=re.findall('\w+@(?:qq|126|163)\.com',y) print(ret3)#第四種寫法: ret3=re.findall('\w+@(qq|126|163)\.com',y) print(ret3)#第五種寫法: ret3=re.findall('(\w+@(?:qq|126|163)\.com)',y) print(ret3)輸出結果:['123@qq.com', 'aaa@163.com', 'bbb@126.com'] [('123@qq.com', 'qq'), ('aaa@163.com', '163'), ('bbb@126.com', '126')] ['123@qq.com', 'aaa@163.com', 'bbb@126.com'] ['qq', '163', '126'] ['123@qq.com', 'aaa@163.com', 'bbb@126.com']

結論:

1、匹配多個字符串中其中一個的方法是使用是小括號+"|",而不是我認為的[http|www],而應該是(http|www)

2、由于findall會返回所有括號內的內容,而我本意只是個約束條件;這個時候可以加上? "?:"?

總結

以上是生活随笔為你收集整理的正则表达式中(?:)的巨大作用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。