Thanks Rob

-----Original Message-----
From: Rob Dixon [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, November 21, 2007 4:33 PM
To: beginners @ perl. org
Cc: Sayed, Irfan (Irfan)
Subject: Re: Regular expression to get part of string

Sayed, Irfan (Irfan) wrote:
>  
> I have one variable which has the value as follows
>  
> "ccvob01    pts/2        Nov 21 12:17  (135.27.157.38 logged in ....."
>  
> Now I want only "ccvob01" from that string so I wrote reg.exp. as 
> follows
>  
> my ($usr1)=($usr =~ m{(.+)\S$}); where $usr contains above string.
>  
> But still I am not getting proper output. 
>  
> Can somebody please guide me.

Hello Irfan

I'm not sure how you expected that to work. It captures all characters
up to but excluding a final non-space character before the end of the
line. You could try

   my ($usr1) = $usr =~ /(.+?)\s/;

which captures all character from the start up to the first whitespace,
or you could just say

   my ($usr1) = $usr =~ /(\S+)/;

which just finds the first sequence of non-space characters and captures
it.

Did you have any reason to use the braces for the regex delimiter
instead of the more usual slashes? It helps your codes readability if
people see what they are expecting.

HTH,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to