正则表达式用于反序列化用分号分隔的字符串


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是一种方法,但我想知道用正则表达式是否更好?

如果您不需要正则表达式的强大功能,也可以将str_replace与数组一起使用:

echo str_replace(array('"{'"', ''";'"', ''"}"'), array("", ", ", ""), $str);

-> A5, A6, A7, varying number of params... eval.in 测试

(?<=''")[^'';]+

试试这个。请参阅演示。

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))