用于反序列化以分号分隔的字符串的正则表达式
Regex for deserializing string separated by semi-colon
我有一个具有以下格式的字符串:-
"{\"A5\";\"A6\";\"A7\";\"varying number of params...\"}"
如何使用PHP将字符串转换成
A5, A6, A7, varying number of params...
我知道 str_replace
是一种方法,但我想知道使用正则表达式是否更好?
(?<=\")[^\;]+
尝试 this.See 演示。
https://regex101.com/r/sH8aR8/53
$re = "/(?<=\\\")[^\\;]+/";
$str = "\"{\"A5\";\"A6\";\"A7\";\"varying number of params...\"}\"";
preg_match_all($re, $str, $matches);
详情:
NODE EXPLANATION
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
\ '\'
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
[^\;]+ any character except: '\', ';' (1 or more
times (matching the most amount possible))
如果您不需要正则表达式的强大功能,您也可以将 str_replace 与数组一起使用:
echo str_replace(array('"{\"', '\";\"', '\"}"'), array("", ", ", ""), $str);
-> A5, A6, A7, varying number of params...
test at eval.in
我有一个具有以下格式的字符串:-
"{\"A5\";\"A6\";\"A7\";\"varying number of params...\"}"
如何使用PHP将字符串转换成
A5, A6, A7, varying number of params...
我知道 str_replace
是一种方法,但我想知道使用正则表达式是否更好?
(?<=\")[^\;]+
尝试 this.See 演示。
https://regex101.com/r/sH8aR8/53
$re = "/(?<=\\\")[^\\;]+/";
$str = "\"{\"A5\";\"A6\";\"A7\";\"varying number of params...\"}\"";
preg_match_all($re, $str, $matches);
详情:
NODE EXPLANATION
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
\ '\'
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
[^\;]+ any character except: '\', ';' (1 or more
times (matching the most amount possible))
如果您不需要正则表达式的强大功能,您也可以将 str_replace 与数组一起使用:
echo str_replace(array('"{\"', '\";\"', '\"}"'), array("", ", ", ""), $str);
-> A5, A6, A7, varying number of params...
test at eval.in