[Israel.pm] how do i cut the first 2 characters/digits of a string?

Shlomo Yona shlomo at cs.haifa.ac.il
Thu Jul 15 02:09:54 PDT 2004


On Thu, 15 Jul 2004, Ernst, Yehuda wrote:


my $string = 'how do i cut the first 2 characters/digits of a string?'
my $cut_string = substr $string,2;

this, however simply copies all characters of $string to
$cut_string except for the first two.


You can do it in place using a regular expression:

$string=~s/^..(.*)$/$1/;
or if you only want to cure the first two digits:
$string=~s/^\d\d(.*)$/$1/;

you might need to add the s modifier, depending on the
content of the string (if you want . to match newlines as
well).




-- 
Shlomo Yona
shlomo at cs.haifa.ac.il
http://cs.haifa.ac.il/~shlomo/



More information about the Perl mailing list