> php教程 > php手册 > 본문

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

WBOY
풀어 주다: 2016-06-21 08:50:33
원래의
805명이 탐색했습니다.

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

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

PHP正则采用:PCRE风格。


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

 

Python:

		
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

#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:

		
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

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中正则表达式还可以这样:

		
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

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

#2 搜索查找Search

Python:

		
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

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

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

PHP:

同上

#3 匹配分割

Python:

		
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

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

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

PHP:

		
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

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>
로그인 후 복사

**/

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

Python:

		
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

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

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

PHP:

			
로그인 후 복사
로그인 후 복사

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>
로그인 후 복사

**/

#5 查找替换

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

python:

		
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

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()返回的是一个以“新字符串和替换的次数”组成的元组,也没有作用到原对象上。

		
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

#替换三次 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)

替换与引用

			
로그인 후 복사
로그인 후 복사

#这里批量替换文章中的图片的路径(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:

		
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사
로그인 후 복사

		
로그인 후 복사

#这里批量替换文章中的图片的路径(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>
로그인 후 복사

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

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

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

 

 

 

 



관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!