> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, December 27, 2001 4:09 PM
> To: [EMAIL PROTECTED]
> Subject: Re: $1, $2, $3
> 
> 
> HI,
> 
> I have a question.
> I want to extract all the information to the left of the last comma.
> If there are 3 commas I would like everything to the left of 
> the third comma.
> 
> $derer  = "Seattle, Washington, USA, NAME";   ## I only need Seattle,
> Washington, USA
> 
> if($derer =~ /(.*?),([.*?,]*)(.*)/g) {             ## I 
> figure Seattle is $1
> Washington is $2 and USA is $3

Square brackets create a character class, which matches ONE character.
So, [.*?,] says to match one character consisting of dot, asterisk,
question mark, or comma. The * after the brackets matches zero or more
times. Since the character at this position is the space before the
"W" in Washington, zero characters are matched and so $2 captures an
empty string and $3 matches from there on.

You said you want to "extract all the information to the left
of the last comma. The way to do that is simply:

   /(.*),/

Using the "longest-leftmost" rule, the parens will capture everything 
to the left of the last comma. (Caveat: "." will not match a newline
unless you use the /s modifier). If there is no comma, the regex
will not match.

Not sure what you're trying to do beyond that.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to