cookbook之字串匹配和搜尋

2021-10-02 04:44:11 字數 1167 閱讀 4275

參考

import datetime

text =

'yao, yao, qie ke nao, jian bing guo zi, yao, lai, yi, tao, '

print(text.find(

'tao'

))#會返回第一次匹配到元素的下標

text2 = datetime.date.today(

)#顯示當前的年月日

text2 = str(text2)

print(type(text2))

print(text2)

import re

text1 =

'11/27/2012'

if re.match(r'\d+\-\d+\-\d+',text2):#原始字串

print(

'ok'

)else:

print(

"no"

)text =

'today is 11/27/2012. pycon starts 3/13/2013.'

a = re.findall(r'\d+/\d+/\d+',text)

print(a)

b = re.findall(r'(\d+)/(\d+)/(\d+)', text)

print(b)

#元組的形式

print(

'-----------------------'

)#使用迭代的方式展示

for m in re.finditer(r'(\d+)/(\d+)/(\d+)', text):

print(m.group(0))

print(m.group(1))

print(m.group(2))

print(m.group(3))

print(m.groups(

)) print(

'+++++++'

)test

= re.compile(r'(\d+)/(\d+)/(\d+)$'

)print(test.match(

'11/27/2012abcdef'

))print(test.match(

'11/27/2012'

).group(0))

cookbook之字串搜尋和替換

參考 import datetime import re from calendar import month abbr text yeah,but no,but yeah,but no,but yeah print text.replace yeah love print text date da...

字串匹配之kmp

kmp主要就是計算字首函式e q max return m void kmp char s,char p int n strlen s int m prefixcomp p,e int k 0 for int i 1 i n i putchar n 習題 試說明如何通過檢查字串pt的字首函式e,來確...

PHP之字串匹配

1 strstr string,search strstr 函式搜尋乙個字串在另乙個字串中的第一次出現。該函式返回字串的其餘部分 從匹配點 如果未找到所搜尋的字串,則返回 false。string 必需。規定被搜尋的字串。search 必需。規定所搜尋的字串。如果該引數是數字,則搜尋匹配數字 asc...