[英]How do I use perl regex to extract the digit value from '[1]'?
My code... 我的代码......
$option = "[1]";
if ($option =~ m/^\[\d\]$/) {print "Activated!"; $str=$1;}
I need a way to drop off the square brackets from $option. 我需要一种方法从$ option中删除方括号。 $str = $1 does not work for some reason.
$ str = $ 1由于某种原因不起作用。 Please advise.
请指教。
要获得1美元的工作,您需要使用括号捕获括号内的值,即:
if ($option =~ m/^\[(\d)\]$/) {print "Activated!"; $str=$1;}
if ($option =~ m/^\[(\d)\]$/) { print "Activated!"; $str=$1; }
Or 要么
if (my ($str) = $option =~ m/^\[(\d)\]$/) { print "Activated!" }
Or 要么
if (my ($str) = $option =~ /(\d)/) { print "Activated!" }
..and a bunch of others. ..和其他一些人。 You forgot to capture your match with ()'s .
你忘了用()的比赛来捕捉你的比赛 。
EDIT: 编辑:
if ($option =~ /(?<=^\[)\d(?=\]$)/p && (my $str = ${^MATCH})) { print "Activated!" }
Or 要么
my $str;
if ($option =~ /^\[(\d)(?{$str = $^N})\]$/) { print "Activated!" }
Or 要么
if ($option =~ /^\[(\d)\]$/ && ($str = $+)) { print "Activated!" }
For ${^MATCH}, $^N, and $+, perlvar . 对于$ {^ MATCH},$ ^ N和$ +, perlvar 。
I love these questions : ) 我喜欢这些问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.