Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use perl regex to extract the digit value from '[1]'?

Tags:

regex

perl

My code...

$option = "[1]";
if ($option =~ m/^\[\d\]$/) {print "Activated!"; $str=$1;}

I need a way to drop off the square brackets from $option. $str = $1 does not work for some reason. Please advise.

like image 292
masterial Avatar asked Dec 12 '22 17:12

masterial


1 Answers

To get $1 to work you need to capture the value inside the brackets using parentheses, i.e:

if ($option =~ m/^\[(\d)\]$/) {print "Activated!"; $str=$1;}
like image 82
Mikesname Avatar answered Mar 04 '23 19:03

Mikesname