• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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
0
shahrahulb
Asked:
shahrahulb
1 Solution
 
bob_anastasiaCommented:
well,

If you JUST want to remove it, a quick dirty way is to do this:

$line =~ s/app//gsi;

this will just remove it.
0
 
shahrahulbAuthor Commented:
app is variable and not constant value
0
 
FishMongerCommented:
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
0
 
FishMongerCommented:
Actually, the . does not need to be escaped in the characture class so, it can be written as;

s/^([^.]+)//
0
 
manav_mathurCommented:
<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/^([^.]+)/) ;



0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now