本文介紹了php preg_replace regex 替換兩個字符串之間的字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有以下問題:我想替換(在 php 中)一個特殊字符,但前提是它位于其他兩個字符之間.它試圖用 preg_replace 找到解決方案,但沒有奏效.
I have the following problem: I want to replace (in php) a special character, but only if it's between two other characters. It tried to find a solution with with preg_replace but it doesn't work.
我想替換每一個;帶有 : 介于 "示例:
I want to replace every ; with a : which is between the " The Examples:
$orig_string= 'asbas;"asd;";asd;asdadasd;"asd;adsas"'
結果應該是:
'asbas;"asd:";asd;asdadasd;"asd:adsas"'
我嘗試了幾個正則表達式,但沒有任何成功......
I tried several regexes but without any succes...
我試過的兩個例子:
$result = preg_replace('(?<=")(.*)(;)(.*)(?=")',':', $str);
$result = preg_replace('.*".*(;).*"',':', $str);
有人可以幫我嗎?
非常感謝
V
推薦答案
在這里你不需要使用環顧四周.可以寫成
You need not use look arounds here. It can be written as
("[^";]*);([^"]*")
替換為 1:2
正則表達式演示
測試
preg_replace ("/("[^";]*);([^"]*")/m", "\1:\2", 'asbas;"asd;";asd;asdadasd;"asd;adsas"' );
=> asbas;"asd:";asd;asdadasd;"asd:adsas"
更新:
;(?!(?:"[^"]*"|[^"])*$)
只需將匹配的 ;
替換為 :
Just replace the matched ;
with :
演示
這篇關于php preg_replace regex 替換兩個字符串之間的字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!