请教perl语言,/(\d{4}(\-?))/这句话什么意思啊?
展开全部
这一正则在尝试寻找 连续 4 个数字, 及 1 个可有可无的 - (横杠)
但值得留意的是, 这里有两组 () , 在括号中能找到匹配的文字时, 会顺序存到 $1, $2 ... 中
$str = "1234";
$str =~ /(\d{4}(\-?))/ ;
if ( $1 and $2 ) { print "\$1 = '$1', \$2 = '$2'. number with - " }
elsif ( $1 ) { print "\$1 = '$1', \$2 = '$2'. number only" }
else { print "not matched" }
print "$/==================$/";
$str = "9876-";
$str =~ /(\d{4}(\-?))/ ;
if ( $1 and $2 ) { print "\$1 = '$1', \$2 = '$2'. number with - " }
elsif ( $1 ) { print "\$1 = '$1', \$2 = '$2'. number only" }
else { print "not matched" }
但值得留意的是, 这里有两组 () , 在括号中能找到匹配的文字时, 会顺序存到 $1, $2 ... 中
$str = "1234";
$str =~ /(\d{4}(\-?))/ ;
if ( $1 and $2 ) { print "\$1 = '$1', \$2 = '$2'. number with - " }
elsif ( $1 ) { print "\$1 = '$1', \$2 = '$2'. number only" }
else { print "not matched" }
print "$/==================$/";
$str = "9876-";
$str =~ /(\d{4}(\-?))/ ;
if ( $1 and $2 ) { print "\$1 = '$1', \$2 = '$2'. number with - " }
elsif ( $1 ) { print "\$1 = '$1', \$2 = '$2'. number only" }
else { print "not matched" }
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询