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:

  1. http://perlmeme.org/howtos/perlfunc/substr.html see example 1c
  2. how take substring of given string until first appearance of specified character?

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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -