Home > Archive > PERL Beginners > November 2007 > Regular expression to get part of string
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Regular expression to get part of string
|
|
| Irfan Sayed 2007-11-21, 7:59 am |
| Hi All,
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.
Regards
Irfan.
| |
| Jeff Pang 2007-11-21, 7:59 am |
| On Nov 21, 2007 6:47 PM, Sayed, Irfan (Irfan) <isayed@avaya.com> wrote:
> Hi All,
>
> 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
try this:
my $usr = "ccvob01 pts/2 Nov 21 12:17 (135.27.157.38 logged
in .....";
my ($usr1) = $usr =~ m{(.+?)\s+};
or
my $usr1 = (split/\s+/,$usr)[0];
>
> my ($usr1)=($usr =~ m{(.+)\S$}); where $usr contains above string.
>
why do you think this can work? you may take another read at `perldoc
perlre` and see what does the \S and $ mean.
| |
| Rob Dixon 2007-11-21, 7:59 am |
| 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
| |
| Irfan Sayed 2007-11-21, 7:59 am |
| =20
Thanks Rob
-----Original Message-----
From: Rob Dixon [mailto:rob.dixon@350.com]=20
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:
> =20
> I have one variable which has the value as follows
> =20
> "ccvob01 pts/2 Nov 21 12:17 (135.27.157.38 logged in ....."
> =20
> Now I want only "ccvob01" from that string so I wrote reg.exp. as=20
> follows
> =20
> my ($usr1)=3D($usr =3D~ m{(.+)\S$}); where $usr contains above string.
> =20
> But still I am not getting proper output.=20
> =20
> 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) =3D $usr =3D~ /(.+?)\s/;
which captures all character from the start up to the first whitespace,
or you could just say
my ($usr1) =3D $usr =3D~ /(\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
| |
| Irfan Sayed 2007-11-21, 7:59 am |
| =20
Thanks Jeff
-----Original Message-----
From: journalfs@gmail.com [mailto:journalfs@gmail.com] On Behalf Of Jeff
Pang
Sent: Wednesday, November 21, 2007 4:26 PM
To: Sayed, Irfan (Irfan)
Cc: beginners @ perl. org
Subject: Re: Regular expression to get part of string
On Nov 21, 2007 6:47 PM, Sayed, Irfan (Irfan) <isayed@avaya.com> wrote:
> Hi All,
>
> 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=20
> follows
try this:
my $usr =3D "ccvob01 pts/2 Nov 21 12:17 (135.27.157.38 logged
in .....";
my ($usr1) =3D $usr =3D~ m{(.+?)\s+};
or
my $usr1 =3D (split/\s+/,$usr)[0];
>
> my ($usr1)=3D($usr =3D~ m{(.+)\S$}); where $usr contains above string.
>
why do you think this can work? you may take another read at `perldoc
perlre` and see what does the \S and $ mean.
|
|
|
|
|