[英]Problems with regex
前一陣子,我建立了以下正則表達式:
~(?:<a.*?</a>|\[url.*?\[/url]|\[/?[^]]++]|</?[^>]++>)(*SKIP)(*FAIL)|\bcdkey\s*-\s*.*\b~is
這匹配不在bbcode或html標記內的所有cdkey-xxx。 到目前為止,一切正常。
但是,當包含bbcodes和html標簽時,我無法使其正常工作。 我以為,除去前部就足夠了,但是我似乎錯了:
~\bcdkey\s*-\s*.*\b~is
有了這個正則表達式,
<a href="https://www.google.de/#q=cdkey-0192xdasas" class="externalURL">https://www.google.de/#q=cdkey-0192xdasas</a>
變
<a href="https://www.google.de/#q=***>
和
[url]https://www.google.de/#q=cdkey-0192xdasas[/url]
變
[url]https://www.google.de/#q=***]
而預期的結果是
<a href="https://www.google.de/#q=***" class="externalURL">https://www.google.de/#q=***</a>
和
[url]https://www.google.de/#q=***[/url]
我不知道如何解決該問題。
所以,我試圖實現的是取代
[url]https://www.google.de/#q=cdkey-0192xdasas[/url]
[url=https://www.google.de/#q=cdkey-0192xdasas]Test[/url]
[img]https://www.google.de/#q=cdkey-0192xdasas[/img]
[url="https://www.google.de/#q=cdkey-0192xdasas"]Test 3[/url]
https://www.google.de/#q=cdkey-0192xdasas
Another plaintext cdkey - bla
<a href="https://www.google.de/#q=cdkey-0192xdasas" class="externalURL">https://www.google.de/#q=cdkey-0192xdasas</a>
<a href='https://www.google.de/#q=cdkey-0192xdasas'>Le Google</a>
同
[url]https://www.google.de/#q=***[/url]
[url=https://www.google.de/#q=***]Test[/url]
[img]https://www.google.de/#q=***[/img]
[url="https://www.google.de/#q=***"]Test 3[/url]
Plaintext https://www.google.de/#q=***
Another plaintext ***
<a href="https://www.google.de/#q=***" class="externalURL">https://www.google.de/#q=***</a>
<a href='https://www.google.de/#q=***'>Le Google</a>
我在您的正則表達式中看到的問題是.*
部分。
您可以在所有匹配項中匹配盡可能多的金額 ,而無需使用s
修飾符。
如果您知道cdkey始終是數字和字母,則可以執行以下操作。
$text = preg_replace('/cdkey\s*-\s*[a-z0-9]+/i', '***', $text);
查看工作演示
我認為邊界\\b
一詞與您內部的語法不兼容。 特別
是連字符和
點星序列不會像通常那樣匹配。
如果您知道什么可能會終止cdkey,則類似以下內容
# \bcdkey\s*-\s*[^<>\[\]"'\s]*
\b cdkey \s* - \s* [^<>\[\]"'\s]*
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.