recognising a special character inside [^]

J

Jason Quek

Hi

What I need to do is to print only '1|2|3'.

#----------------------------------------------------------------
@array = (
'1',
'1|2',
'1|2|3',
'1|2|3|4',
'1|2|3|4|5',
'1|2|3|4|5|6'
);

$string = '1|2';

foreach (@array)
{
if ($_ =~ /^$string\|([^\|]*)$/)
{
print "$_\n";
}
}
#----------------------------------------------------------------

But the above code prints the entire array out.

I believe the problem lies with ([^\|]*).

How do I correctly tell Perl to recognise the pipe | character inside
([^\|]*)

Any help would be appreciated.




Jason Q.
 
B

Brian Wakem

Jason Quek said:
Hi

What I need to do is to print only '1|2|3'.

#----------------------------------------------------------------
@array = (
'1',
'1|2',
'1|2|3',
'1|2|3|4',
'1|2|3|4|5',
'1|2|3|4|5|6'
);

$string = '1|2';

foreach (@array)
{
if ($_ =~ /^$string\|([^\|]*)$/)
{
print "$_\n";
}
}
#----------------------------------------------------------------

But the above code prints the entire array out.

I believe the problem lies with ([^\|]*).

How do I correctly tell Perl to recognise the pipe | character inside
([^\|]*)

Any help would be appreciated.

if ($_ =~ /^\Q$string\E\|([^\|]*)$/) {
....
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top