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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

正则表达式(一) search

發布時間:2023/12/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 正则表达式(一) search 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

何為正則表達式?

正則表達式,又稱規則表達式,英文名為RegularExpression,在代碼中常簡寫為regex、regexp或RE,是計算機科學的一個概念。正則表通常被用來檢索、替換那些符合某個模式(Patterns)的文本。

正則表達式是對字符串(包括普通字符(例如,a 到 z 之間的字母)和特殊字符(稱為“元字符”))操作的一種邏輯公式,就是用事先定義好的一些特定字符、及這些特定字符的組合,組成一個“規則字符串”,這個“規則字符串”用來表達對字符串的一種過濾邏輯。正則表達式是一種文本模式,模式描述在搜索文本時要匹配的一個或多個字符串。

Find Patterns in Text(文本查找模式)

關于re的最常見用法是在文本中搜索模式

常用re.search()

string進行搜索,成功返回Match object, 失敗返回None, 只匹配一個

search(pattern, string, flags=0):

"""Scan through string looking for a match to the pattern, returning

a match object, or None if no match was found."""


例子1:

import repatterns = [ 'this', 'that' ] text = 'Does this text match the pattern?'for pattern in patterns:print 'Looking for "%s" in "%s" ->' % (pattern, text),if re.search(pattern, text):print 'found a match!'else:print 'no match'

? ? ???例子2:

import repattern='this' #AB text='Does this text match the pattern ?'match=re.search(pattern,text)if match: s = match.start()e = match.end()print('Found "%s" in "%s" from %d to %d("%s")' % \(pattern,text, s, e, text[s:e])) else:print('No found')

參考:

[1]https://baike.sogou.com/v107588.htm?fromTitle=正則表達式

[2]https://pymotw.com/2/re/#finding-patterns-in-text



總結

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

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