Rob Coops wrote:
> On Tue, Nov 18, 2008 at 9:52 AM, howa <[EMAIL PROTECTED]> wrote:
>
>> 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?
>
> This does not work because $ denotes the end of the string. So after $ there
> is no more string, matching something at the end of the string you would do
> by writting <something>$ so in your case: (.*)&$ (which will only match the
> line with & in there. If you want to capture both lines you end up doing
> somehting like this: (.*)&{0,1}$
>
> Note: there are other ways to match both lines, just like perl it self
> regular expressions quite often ofer more then one way to do the same thing.
That is wrong. a dollar sign in a character class like [$&] matches a dollar
sign, not an end of line, and that is why the OP's regex doesn't work.
My preference would be to collect everything that wasn't an ampersand:
/([^&]+)/
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/