PERL – REGEX – remove string after get position number


sub string_cut_after_number {

    my $string = $_[0];
    
    $string =~ m/(\d+)/; # regex, find number
    
    my $number_pattern = $1;
    
    if($number_pattern) { # if string had number
        
        my $position = index($string, $number_pattern);
        
        my $cut_from_number_index = substr $string, 0, $position;
        
        return $cut_from_number_index;
    }
    
    return $string;
}

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s