howa wrote:
Hello,

Hello,

I have two strings:

1. abc
2. abc&


The line of string might end with "&" or not, so I use the expression:

(.*)[$&]


Why it didn't work out?

$ perl -le'
for ( "abc", "abc&" ) {
    print;
    print $1 if /(.*)[$&]/;
    }
'
abc
Unmatched [ in regex; marked by <-- HERE in m/(.*)[ <-- HERE ]/ at -e line 4.


It didn't work because the variable $& is interpolated so the regular expression ends up as '(.*)[]', which is an invalid character class, or $& contains some character that does not match your string.

What are you trying to match? If you want an optional '&' match at the end of the string then perhaps you want:

$ perl -le'
for ( "abc", "abc&" ) {
    print;
    print $1 if /(.*)&?$/;
    }
'
abc
abc
abc&
abc&




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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


Reply via email to