Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个要拆分的字符串。但是分隔符是在运行时确定的,所以我需要将它作为变量传递。
类似的东西my @fields = split(/$delimiter/,$string);不起作用。有什么想法吗?
my @fields = split(/$delimiter/,$string);
输入:
abcd|efgh|23
预期输出:
abcd efgh 23
您需要转义分隔符,因为它是正则表达式中的特殊字符。
选项1:
$delimiter = quotemeta($delimiter); my @fields = split /$delimiter/, $string;
选项 2:
my @fields = split /\Q$delimiter/, $string;