regex - EXtracting sub-string in Perl? -
i have string in variable:
$mystr = "some text %parts/dir1/dir2/myfile.abc more text";
now %parts literally present in string, not variable or hash. want extract sub-string %parts/dir1/dir2/myfile.abc
it. created following reg expression. beginner in perl. please let me know if have done wrong.
my $local_file = substr ($mystr, index($mystr, '%parts'), index($mystr, /.*%parts ?/));
i tried this:
my $local_file = substr ($mystr, index($mystr, '%parts'), index($mystr, /.*%parts' '?/));
but both give nothing if print $local_file
. might wrong here? thank you.
update: referred following sites using method:
the index
function returns first index of occurrence of substring in string, else -1
. has nothing regular expressions.
regular expressions applied string bind operator =~
.
to extract matched area of regular expression, enclose pattern in parens (a capture group). matched substring available in $1
:
my $str = "some text %parts/dir1/dir2/myfile.abc more text"; if ($str =~ /(%parts\s+)/) { $local_file = $1; ...; # } else { die "the match failed"; # else }
the \s
character class match every non-space character.
to learn regular expressions, can @ perlretut
.
Comments
Post a Comment