PHP および Python に関連する通常の関数の例

WBOY
リリース: 2016-06-21 08:50:33
オリジナル
805 人が閲覧しました

文字列処理を行うときに、文字列処理関数で目的を達成できない場合は、正規表現を使用して目的を達成します。

正規表現が使用される一般的な状況には、マッチング、検索、セグメント化、検索、置換が含まれます。以下では、これらの状況をそれぞれ PHP と Python で実装し、比較します。

通常の PHP は PCRE スタイルを採用しています。


#1 Match Math (そして結果を取得)(ここでは一致結果を取得する必要があります。これは結果を取得しないこととは異なります)

Python:

		
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

#coding:utf-8 import re strs = '私はあなたを愛しています。ははは、フェハ パット = re.コンパイル( r'^.*?(w+).*?$',re.) print patt.match( strs ).group(1) #Output P

match の機能は検索ではなく照合プロセスであることを説明します。この方法は完全一致ではありません。完全一致が必要な場合は、式の最後に境界一致文字「$」を追加できます。

PHP:

		
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

php $strs = '愛しています o?ナハハフェハ'; preg_match('/^.*?(w+).*?$/i',$strs,$m); var_dump( $m[1]);

#出力: 文字列 'P' (長さ=1)

注: preg_match() は Python の match と同じで、最初の一致の後で検索を停止します。 Preg_match_all() はこれとは異なり、件名を最後まで検索します。

実際、PHP の正規表現は次のようにすることもできます:

		
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

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

#2 検索検索

Python:

		
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

パット = re.コンパイル(r'(w+)',re.I) print パット.検索(strs).グループ(1) #出力 P

検索方法は同じです。見つかった場合は、すぐに返されます。PHP では、preg_match(_all) を使用して検索を実行します。

PHP:

同上

#3 マッチセグメンテーション

Python:

		
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

パット = re.コンパイル(r'w+'re.) for 私は パット .分割(strs): #出力には Unicode オブジェクトを使用する必要があることに注意してください print unicode (i,'utf-8') #上記の出力 」「 私は あなた あなた 知っています ?」 はは ははは'''

PHP では、preg_split() を使用して

を実現できます。

PHP:

		
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

php $strs = '愛しています o?ははは、feha'; $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 学習者の迅速な成長を支援します!