Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the nth character or digit in a string using REGEX in Perl

Tags:

regex

perl

I would like to find nth occurence of a digit or character using regex in perl.

For example: If the string is:

$string = 'abdg2jj4jdh5jfj6'

i need to match the digit 5 which is the 3rd digit.

How can i do it with regex.

like image 346
kailash19 Avatar asked Dec 07 '22 16:12

kailash19


1 Answers

my $string = "abdg2jj4jdh5jfj6";
my @myArray = ($string =~ /(\d)/g);
print "$myArray[2]\n";

Output:

5

like image 148
Brian Roach Avatar answered Jan 26 '23 00:01

Brian Roach