❶ 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