python如何匹配指定的文字

python 根据正则表达式提取指定的内容
正则表达式是极其强大的,利用正则表达式来提取想要的内容是很方便的事。
实例代码
import re# 正则表达式是极其强大的,利用正则表达式来提取想要的内容是很方便的事。# 下面演示了在python里,通过正则表达式来提取符合要求的内容。有几个要注意# 的地方就是:# [1] 要用()将需要的内容包含起来# [2] 编号为0的group是整个符合正则表达式的内容,编号为1的是第一个(及对应# 的)包含的内容# @param regex: regular expression, use () to group the result# 正则表达式,用()将要提取的内容包含起来# @param content: # @param index: start from 1, depends on the \p regex's ()# 从1开始,可以通过数(来得到,其中0是全部匹配# @return: the first match of the \p regex# 只返回第一次匹配的内容def extractData(regex, content, index=1): r = '0' p = re.compile(regex) m = p.search(content) if m: r = m.group(index) return r regex = r'第(.*)场雪'content = '2002年的第一场雪'index = 1print extractData(regex, content, index)

⑵ python 通配符匹配如何进行

我觉得你可以使用正则表达式,比如:

importre
p=re.compile('.*.sougou.com')
s="ht.sougou.com!@#$%^&*()"

ifp.match(s):
print'True'

⑶ python正则表达式匹配

Python 正则表达式
正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字内符串是否容与某种模式匹配。
Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式。
re 模块使 Python 语言拥有全部的正则表达式功能。
compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象。该对象拥有一系列方法用于正则表达式匹配和替换。
re 模块也提供了与这些方法功能完全一致的函数,这些函数使用一个模式字符串做为它们的第一个参数。

⑷ 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正则中我应该怎么匹配 '+']&&&&['-'] 这个字符呢

正则的话

a="3****['+']&&&&['-']"
b = re.search("'\+'\]&&&&\['\-'\]",a)
另外为什么非要用正则,直接string find不是更容易么?

⑹ python 正则匹配 或者怎么写

python 正则表达式
http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

⑺ python中的正则表达式中的 "|"

Python中re.findall()函数是要求正则表达式在捕获第0组数据时,要在正则表达式上加小括号才能捕获.

也就是说如果你要获取整个正则表达式匹配的数据(你这里是电子邮箱地址),需要在正则表达式外面加小括号,

然后取第0捕获组的数据(你这里是[x[0] for x in zhengze]),

因为findall函数把每一个匹配的多个捕获组(就是你正则表达式中的小括号中)的数据放到一个元组里,所以要用for循环把第0捕获组的数据取出来.

具体程序改进如下

>>>zhengze=re.findall("([A-Za-z0-9]+@(163|qq|gmail).com)",txt)

>>>[x[0]forxinzhengze]

结果就是你要的邮箱列表了.

⑻ python 正则匹配^和$使用问题

^表示开始的意思。$表示结尾。match这个东西很奇怪。它是从开始做匹配的。 通常我会使用search而不是用match。 findall也是用search的机制。因为它是findall所以,就不可能用^作为字符串开头。因为字符串只有一个开头,但是有很多的”电话号码“。同样一个字符串也只有一个结尾 。你如果有结尾也不合适。

⑼ python正则表达式匹配或匹配后有空字符的问题。

看了你的追问,我就很纳闷了,你既然只是要有取得正则匹配到的值,干嘛不在for循环内里加一个容判断啊

x=0
forimgurlinimglist:
ifimgurl:#判断是否有值
urllib.urlretrieve(imgurl,"%s.jpg"%x)
#不知道你这个x干嘛用的
x+=1

⑽ python 正则怎么匹配a到b的内容,包括A和B

importre

defmy_search(text,a,b):
regexp=r'(%s.*%s)'%(a,b)
m=re.search(regexp,text)
ifm:
returnm.group(1)
return""

#my_search返回值即为匹配内容,如下:
my_search("123A1234567890B1234","A","B")