Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to find the index of the first number in a string using perl

Tags:

perl

I'm trying to find the index of the first occurrence of a number from 0-9.

Let's say that:

 $myString = "ABDFSASF9fjdkasljfdkl1"

I want to find the position where 9 is.

I've tried this:

print index($myString,[0-9]);

And:

print index($myString,\d);
like image 261
Mr_Brightside1111 Avatar asked Nov 02 '14 07:11

Mr_Brightside1111


1 Answers

Use regex Positional Information:

use strict;
use warnings;

my $myString = "ABDFSASF9fjdkasljfdkl1";

if ($myString =~ /\d/) {
    print $-[0];
}

Outputs:

8
like image 100
Miller Avatar answered Sep 22 '22 12:09

Miller