how to parse string from [?

S

sam

Hi,

I have the following string need to be parsed in perl,
"sshd(pam_unix)[2009]"

I want to extract the first the word up to "[", what is the regular
expression can do that?
The following regular express does not work:
"m/^([^\[]*)\[.*]$"

Thanks
sam
 
G

Gunnar Hjalmarsson

sam said:
I have the following string need to be parsed in perl,
"sshd(pam_unix)[2009]"

I want to extract the first the word up to "[", what is the regular
expression can do that?
The following regular express does not work:
"m/^([^\[]*)\[.*]$"

No? Exactly what did you expect it to do, and what did it do? Please
post complete code.

(I take for granted that the missing trailing slash is a typo when you
retyped the code into the message. Don't retype - copy and paste!)
 
S

sam

Jim said:
Hi,

I have the following string need to be parsed in perl,
"sshd(pam_unix)[2009]"

I want to extract the first the word up to "[", what is the regular
expression can do that?
The following regular express does not work:
"m/^([^\[]*)\[.*]$"

Thanks
sam


"does not work" is a little vague. You need to terminate the regular
expression with another '/'. Except for that, your expression works for
me. You can simplify it a little by 1) not escaping ']' in the character
class (it doesn't need it), and 2) not including the extraneous '\[.*]$'
at the end, unless you are trying to eliminate lines that do not match
that part.

Please post a complete, working program and tell us what it is doing
that "does not work".

Thanks for your suggestion, here is my simplified perl script:
#!/usr/bin/perl -w
use strict; while (<DATA>) { print "$1\n" if "m/^([^\[]*)\[.*$/"; } __DATA__
sshd(pam_unix)[2009]

Note, there is no "\n" at the end of the "sshd(..)[2009]" string.

thanks
sam
 
P

Paul Lalli

Jim Gibson wrote:

Thanks for your suggestion, here is my simplified perl script:
#!/usr/bin/perl -w
use strict; while (<DATA>) { print "$1\n" if "m/^([^\[]*)\[.*$/"; } __DATA__
sshd(pam_unix)[2009]

Note, there is no "\n" at the end of the "sshd(..)[2009]" string.

Why are you putting quotes around your whole regexp? You're suddenly
asking if this big long string returns a true value, rather than whether
or not a pattern match succeeds. Get rid of those quotes.

print "$1\n" if /^([^[]*)/;


Paul Lalli
 
S

sam

Paul said:
Jim Gibson wrote:

Thanks for your suggestion, here is my simplified perl script:
#!/usr/bin/perl -w
use strict; while (<DATA>) { print "$1\n" if "m/^([^\[]*)\[.*$/"; } __DATA__
sshd(pam_unix)[2009]

Note, there is no "\n" at the end of the "sshd(..)[2009]" string.


Why are you putting quotes around your whole regexp? You're suddenly
asking if this big long string returns a true value, rather than whether
or not a pattern match succeeds. Get rid of those quotes.

print "$1\n" if /^([^[]*)/;
Thanks, it works now.
sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top