文字列処理を行うときに、文字列処理関数で目的を達成できない場合は、正規表現を使用して目的を達成します。
正規表現が使用される一般的な状況には、マッチング、検索、セグメント化、検索、置換が含まれます。以下では、これらの状況をそれぞれ 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);">'我爱'</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);">'你'</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);">'你'</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);">'知'</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);">'吗'</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);">'?'</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);">'哈哈'</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);">'哈'</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);">'P'</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);">'y'</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);">'t'</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);">'h'</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);">'o'</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);">'n'</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);">'fe'</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);">'P'</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);">'y'</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);">'t'</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);">'h'</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);">'o'</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);">'n'</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);">'fe'</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);">'我爱999你999你999知999吗999?999哈哈999哈'</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 另外有一点需要注意的是:对于处理字符串能用字符串函数处理的就用函数处理,千万别用正则。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

SublimeText3 中国語版
中国語版、とても使いやすい

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません
