yeah... Japhy almost always beats me in the sensible algorithms.. his rindex idea is the most efficient way, then his regex with the positive look ahead assertion, then my crappy poorly thoughtout regex. ;)
> -----Original Message----- > From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]] > Sent: Thursday, April 11, 2002 1:40 PM > To: [EMAIL PROTECTED] > Cc: [EMAIL PROTECTED] > Subject: Re: Re: Matching last comma (not last character) > > > On , [EMAIL PROTECTED] said: > > >I used the regex in the end, the list will only ever be a > few elements > >long (and the script doesn't need to be efficient). Can't say I > >understand it completely though (I was with you up until the first > >comma) :) > > Ok, here's an explanation: > > >> s/,(?=[^,]*$)/, or/; > > The (?=...) means "look ahead for...", so after the regex has > matched a > comment, it checks to see if the comma is followed by zero or more > NON-commas and the end of the string. Because that part is > in (?=...), > the regex will make sure that sub-pattern matches WITHOUT > ADVANCING IN THE > STRING. Compare: > > ($x = "a, b, c, d") =~ s/,(?=[^,]*$)/, or/; print $x; > # a, b, c, or d > > ($x = "a, b, c, d") =~ s/,[^,]*$/, or/; print $x; > # a, b, c, or > > If you don't want the look-ahead, you could do > > ($x = "a, b, c, d") =~ s/,([^,]*$)/, or$1/; print $x; > # a, b, c, or d > > but the point of the look-ahead is to avoid having to do that. > > -- > Jeff "japhy" Pinyan [EMAIL PROTECTED] > http://www.pobox.com/~japhy/ > RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] ---------------------------------------------------------------------------- -------------------- The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]