我有一个字符串,字符串的一些内容用双引号引起来。 例如:
test_case_be "+test+tx+rx+path"
对于上面的输入,我想将整个字符串分成两部分:
- 双引号外的字符串 [
test_case_be
] 我想存储在$temp1
中。 - 双引号内的字符串 [
+test+tx+rx+path
] 我想将其存储在$temp2
中。
有人可以帮我提供有关如何执行上述操作的示例代码吗?
最佳答案
这个可以做到:
my $input_string = qq(test_case_be "+test+tx+rx+path");
my $re = qr/^([^"]+)"([^"]+)"/;
# Evaluating in list context this way affects the first variable to the
# first group and so on
my ($before, $after) = ($input_string =~ $re);
print <<EOF;
before: $before
after: $after
EOF
输出:
before: test_case_be
after: +test+tx+rx+path
关于Perl:读取字符串中双引号之间的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14270748/