我认为这应该很简单,在同一行上用双引号/单引号匹配字符串
例如,以下所有字符串都在同一行上
"hello" 'world' 'foo' "bar"
我有
print /(".*?")|('.*?')/g;
但是我遇到了以下错误
Use of uninitialized value in print at ...
以下内容将返回您提到的警告:
use strict;
use warnings;
my $str = q{"hello" 'world' 'foo' "bar"};
print $str =~ /(".*?")|('.*?')/g;
这是因为您的正则表达式将仅匹配捕获组中的一个。另一个将不匹配,因此将返回undef
。
以下将演示:
while ($str =~ /(".*?")|('.*?')/g) {
print "one = " . (defined $1 ? $1 : 'undef') . "\n";
print "two = " . (defined $2 ? $2 : 'undef') . "\n";
print "\n";
}
输出:
one = "hello"
two = undef
one = undef
two = 'world'
one = undef
two = 'foo'
one = "bar"
two = undef
要获得所需的行为,只需将捕获组放在整个表达式周围。
print $str =~ /(".*?"|'.*?')/g;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句