❶ python裡面match和search的區別
不知道你是不是說的python
re模塊的match和search方法:
1、match
re.match(pattern,
string[,
flags])
從首字母開始開始匹配,string如果包含pattern子串,則匹配成功,返回Match對象,失敗則返回None,若要完全匹配,pattern要以$結尾。
2、search
re.search(pattern,
string[,
flags])
若string中包含pattern子串,則返回Match對象,否則返回None,注意,如果string中存在多個pattern子串,只返回第一個。
若匹配成功,match()/search()返回的是Match對象,獲取匹配結果需要調用Match對象的group()、groups或group(index)方法。
❷ python 中re.search()的問題
<_sre.SRE_Match object at 0x01FED5D0> 返回的是一個匹配對象 ,調用對象的group方法獲得字元串
>>> import re
a = re.search('\d+','231422sadf')
>>> a.group(0)
'231422'
❸ 請教關於用Python腳本實現ldapsearch 查詢
需要使用模塊ldap,示例代碼
importldap
l=ldap.initialize('ldap://ldapserver')
username="uid=%s,ou=People,dc=mydotcom,dc=com"%username
password="mypassword"
try:
l.protocol_version=ldap.VERSION3
l.simple_bind_s(username,password)
valid=True
exceptException,error:
❹ python 正則表達式 search findall
因為當正則表達式中存在分組的時候,findall返回的結果是分組對應的內容,因為(,\d{3})沒有匹配,所以返回的數組為空.
解決辦法是加上小括弧,象這樣 ^(\d{1,3}(,\d{3})*)$ 就應該能返回數字了
❺ Python re.search 問題
你要寫原生字元串:re.search(r"(d{3})1","123123") 前面加個r
或者用兩個斜杠表示一個真實的斜杠:re.search("(d{3})\1","123123")
具體參考正則表達式語法
❻ Python正則表達式的幾種匹配用法
下面列出: 1.測試正則表達式是否匹配字元串的全部或部分regex=ur"" #正則表達式
if re.search(regex, subject): do_something()else: do_anotherthing() 2.測試正則表達式是否匹配整個字元串 regex=ur"/Z" #正則表達式末尾以/Z結束
if re.match(regex, subject): do_something()else: do_anotherthing() 3.創建一個匹配對象,然後通過該對象獲得匹配細節(Create an object with details about how the regex matches (part of) a string) regex=ur"" #正則表達式
match = re.search(regex, subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing() 4.獲取正則表達式所匹配的子串(Get the part of a string matched by the regex) regex=ur"" #正則表達式
match = re.search(regex, subject)if match: result = match.group()else: result ="" 5. 獲取捕獲組所匹配的子串(Get the part of a string matched by a capturing group) regex=ur"" #正則表達式
match = re.search(regex, subject)if match: result = match.group(1)else: result ="" 6. 獲取有名組所匹配的子串(Get the part of a string matched by a named group) regex=ur"" #正則表達式
match = re.search(regex, subject)if match:result = match.group"groupname")else:result = "" 7. 將字元串中所有匹配的子串放入數組中(Get an array of all regex matches in a string) result = re.findall(regex, subject) 8.遍歷所有匹配的子串(Iterate over all matches in a string) for match in re.finditer(r"<(.*?)/s*.*?//1>", subject) # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() 9.通過正則表達式字元串創建一個正則表達式對象(Create an object to use the same regex for many operations) reobj = re.compile(regex) 10.用法1的正則表達式對象版本(use regex object for if/else branch whether (part of) a string can be matched) reobj = re.compile(regex)if reobj.search(subject): do_something()else: do_anotherthing() 11.用法2的正則表達式對象版本(use regex object for if/else branch whether a string can be matched entirely) reobj = re.compile(r"/Z") #正則表達式末尾以/Z 結束
if reobj.match(subject): do_something()else: do_anotherthing() 12.創建一個正則表達式對象,然後通過該對象獲得匹配細節(Create an object with details about how the regex object matches (part of) a string) reobj = re.compile(regex) match = reobj.search(subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing() 13.用正則表達式對象獲取匹配子串(Use regex object to get the part of a string matched by the regex) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group()else: result ="" 14.用正則表達式對象獲取捕獲組所匹配的子串(Use regex object to get the part of a string matched by a capturing group) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group(1)else: result ="" 15.用正則表達式對象獲取有名組所匹配的子串(Use regex object to get the part of a string matched by a named group) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group("groupname")else: result ="" 16.用正則表達式對象獲取所有匹配子串並放入數組(Use regex object to get an array of all regex matches in a string) reobj = re.compile(regex) result = reobj.findall(subject) 17.通過正則表達式對象遍歷所有匹配子串(Use regex object to iterate over all matches in a string) reobj = re.compile(regex)for match in reobj.finditer(subject): # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()字元串替換 1.替換所有匹配的子串 #用newstring替換subject中所有與正則表達式regex匹配的子串
result = re.sub(regex, newstring, subject) 2.替換所有匹配的子串(使用正則表達式對象) reobj = re.compile(regex) result = reobj.sub(newstring, subject) 字元串拆分 1.字元串拆分 result = re.split(regex, subject) 2.字元串拆分(使用正則表示式對象) reobj = re.compile(regex) result = reobj.split(subject)
❼ python正則 search()的小問題!!!
最後一個字元串中明顯沒有和「yes no」匹配的子字元串啊,需要完全匹配的字元串才算是匹配的,包括順序也要一樣。是不是程序寫多了,頭懵了
❽ python語言里match()和search()的區別是什麼啊
Match是從字元串的起始位置開始匹配,如果匹配成功的話,就返回第一個對象;
Search工作方式與match比較相似,只要search從字元串的任意位置開始匹配,並返回第一個匹配的對象。
區別:Match()函數只檢測RE是不是在string的開始位置匹配,search()會掃描整個string查找匹配;換句話來講,match()只有在0位置匹配成功的話才會返回,如果不是開始位置匹配成功的話,match()就返回none,這就是它們之間的區別。
❾ Python裡面search和match的區別
match()函數只檢測RE是不是在string的開始位置匹配, search()會掃描整個string查找匹配, 也就是說match()只有在0位置匹配成功的話才有返回,如果不是開始位置匹配成功的話,match()就返回none
例如:
print(re.match(『super』, 『superstition』).span())會返回(0, 5)
而print(re.match(『super』, 『insuperable』))則返回None
search()會掃描整個字元串並返回第一個成功的匹配
例如:print(re.search(『super』, 『superstition』).span())返回(0, 5)
print(re.search(『super』, 『insuperable』).span())返回(2, 7)
❿ Python正則表達式match和search區別,舉個例子
re.match 嘗試從字元串的起始位置匹配一個模式,如果不是起始位置匹配成功的話,match()就返回none。
re.search 掃描整個字元串並返回第一個成功的匹配。
re.match只匹配字元串的開始,如果字元串開始不符合正則表達式,則匹配失敗,函數返回None;而re.search匹配整個字元串,直到找到一個匹配。
實例:
importre
line="Catsaresmarterthandogs";
matchObj=re.match(r'dogs',line,re.M|re.I)
ifmatchObj:
print("match-->matchObj.group():",matchObj.group())
else:
print("Nomatch!!")
matchObj=re.search(r'dogs',line,re.M|re.I)
ifmatchObj:
print("search-->matchObj.group():",matchObj.group()
else:
print("Nomatch!!")
運行結果:
Nomatch!!
search-->matchObj.group():dogs