shahrahulb
asked on
extract
i have string in followin format
app.txt_050405-10:22:25 is up and running now
how do i extract the "app" from string
app.txt_050405-10:22:25 is up and running now
how do i extract the "app" from string
ASKER
app is variable and not constant value
When you say 'extract' do you mean you want to remove it or put it into another variable?
#!perl -w
$str = 'app.txt_050405-10:22:25';
$str =~ s/^([^\.]+)//;
$extracted = $1;
print "extracted/removed '$extracted' from $str$/";
C:\Temp>test.pl
extracted/removed 'app' from .txt_050405-10:22:25
#!perl -w
$str = 'app.txt_050405-10:22:25';
$str =~ s/^([^\.]+)//;
$extracted = $1;
print "extracted/removed '$extracted' from $str$/";
C:\Temp>test.pl
extracted/removed 'app' from .txt_050405-10:22:25
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<quote>
$str =~ s/^([^\.]+)//;
$extracted = $1;
print "extracted/removed '$extracted' from $str$/";
</quote>
if $str contains all '.' (or if it starts with a '.'), this may match a stale $1 from a previous successfull match. As I've heard, this is not a good idea. $n should be accessed only after checking whether the match was successful.
$extracted = $1 if ($str =~ m/^([^.]+)/) ;
$str =~ s/^([^\.]+)//;
$extracted = $1;
print "extracted/removed '$extracted' from $str$/";
</quote>
if $str contains all '.' (or if it starts with a '.'), this may match a stale $1 from a previous successfull match. As I've heard, this is not a good idea. $n should be accessed only after checking whether the match was successful.
$extracted = $1 if ($str =~ m/^([^.]+)/) ;
If you JUST want to remove it, a quick dirty way is to do this:
$line =~ s/app//gsi;
this will just remove it.