Home > php教程 > php手册 > PHP、Python 相关正则函数一点实例

PHP、Python 相关正则函数一点实例

WBOY
Release: 2016-06-21 08:50:33
Original
864 people have browsed it

当我们在做字符串处理时,如果字符串处理函数不能实现我们想要的时,我们就借助正则来帮助我们实现了。

一般使用正则的情况有:匹配、查找、分割、查找并替换,下面我们就将这几种情况分别用PHP和Python语言来实现,并做一下对比。

PHP正则采用:PCRE风格。


#1 匹配Math(并获取出结果)(注意这里是要获取出匹配结果的,与不获取结果有所不同)

 

Python:

		
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

#coding:utf-8 import re strs = '我爱P你y你t知h吗o?n哈哈fe哈' patt = re.compile(r'^.*?(\w+).*?$',re.I) print patt.match(strs).group(1) #输出 P

说明match的作用是一个匹配的过程,不是查找。这个方法并不是完全匹配,想要完全匹配,可以在表达式末尾加上边界匹配符'$'。

PHP:

		
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

php $strs = '我爱P你y你t知h吗o?n哈哈fe哈'; preg_match('/^.*?(\w+).*?$/i',$strs,$m); var_dump($m[1]);

#输出:string 'P' (length=1)

说明:preg_match()与python中的match一样,在第一次匹配后 将会停止搜索。而preg_match_all()不同于此, 它会一直搜索subject 直到到达结尾。

实际上,在PHP中正则表达式还可以这样:

		
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

preg_match('/(\w+)/',$strs,$m);

#2 搜索查找Search

Python:

		
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

patt = re.compile(r'(\w+)',re.I) print patt.search(strs).group(1) #输出 P

说明search方法一样,若查找到了就立即返回,否则一直搜索到字符串末尾,在PHP中可以使用preg_match(_all) 来实现。

PHP:

同上

#3 匹配分割

Python:

		
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

patt = re.compile(r'\w+',re.I) for i in patt.split(strs): #注意这里要使用unicode对象输出 print unicode(i,'utf-8') #以上输出 ''' 我爱 哈哈 哈'''

在PHP中可以使用preg_split()来实现

PHP:

		
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

php $strs = '我爱P你y你t知h吗o?n哈哈fe哈'; $m = preg_split('/\w+/i',$strs); var_dump($m);

/**输出:

		<b style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">array</span></b>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">  0 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;我爱&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=6)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">  1 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;你&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=3)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">  2 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;你&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=3)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">  3 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;知&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=3)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">  4 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;吗&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=3)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">  5 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;?&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=3)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">  6 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;哈哈&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=6)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">  7 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;哈&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=3)</span></i>
Copy after login

**/

#4 搜索查找所有结果(ALL)

Python:

		
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

print patt.findall(strs) #输出 ['P', 'y', 't', 'h', 'o', 'n', 'fe']

在PHP中可使用preg_match_all() 来实现

PHP:

			
Copy after login
Copy after login

php $strs = '我爱P你y你t知h吗o?n哈哈fe哈'; preg_match_all('/(\w+)/i',$strs,$m); var_dump($m);

/**

			<b style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">array</span></b>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">  0 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">    </span><b style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">array</span></b>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      0 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;P&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      1 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;y&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      2 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;t&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      3 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;h&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      4 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;o&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      5 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;n&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      6 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;fe&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=2)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">  1 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">    </span><b style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">array</span></b>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      0 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;P&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      1 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;y&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      2 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;t&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      3 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;h&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      4 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;o&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      5 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;n&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=1)</span></i>
<span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">      6 </span><span class="com" style="color: rgb(136, 0, 0);">=></span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;fe&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=2)</span></i>
Copy after login

**/

#5 查找替换

实际上finditer()方法在python中不是查找替换,它仅是返回一个顺序访问每一个匹配结果(Match对象)的迭代器

python:

		
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

for i in patt.finditer(strs): print i.group() #以上输出 ''' P y t h o n fe '''

这和PHP中的preg_filter()有所不同,preg_filter()与preg_replace()都是执行一个正则表达式的搜索和替换。在python中正则方法中,用于查找替换的是:sub()与subn()。

需要注意的是sub()返回的一个新字符串,不是作用在原对象上。

subn()返回的是一个以“新字符串和替换的次数”组成的元组,也没有作用到原对象上。

		
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

#替换三次 print patt.sub('99',strs,3) #输出 '我爱99你99你99知h吗o?n哈哈fe哈'

print patt.subn('99',strs) #输出:是一个元组('我爱99你99你99知99吗99?99哈哈99哈',7)

替换与引用

			
Copy after login
Copy after login

#这里批量替换文章中的图片的路径(old_c 是文章的内容)

img_dir = 'test'

img_patt = re.compile('src=".*?/(\w+\.\w+)"')

new_c = img_patt.sub(r'src="./%s/\1"'%img_dir,old_c)

PHP:

		
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

		
Copy after login

#这里批量替换文章中的图片的路径(old_c 是文章的内容)

img_dir = 'test' img_patt = re.compile('src=".*?/(\w+\.\w+)"') new_c = img_patt.sub(r'src="./%s/\1"'%img_dir,old_c)

#输出:

		<span class="com" style="color: rgb(136, 0, 0);">string</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">&#39;我爱999你999你999知999吗999?999哈哈999哈&#39;</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);"> </span><i style="line-height: 25px;"><span class="com" style="color: rgb(136, 0, 0);">(length=51)</span></i>
Copy after login

另注   1 对于正则的基础知识可以GOOGLE一下,Python正则的基础知识也可以GOOGLE一下。

          2 对于更多关于PHP PCRE风格的正则基础,可以参看:http://cn2.php.net/manual/zh/regexp.introduction.php

          3 另外有一点需要注意的是:对于处理字符串能用字符串函数处理的就用函数处理,千万别用正则。

 

 

 

 



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template