Re: help with a regex and greediness

From: Stuart White (poovite_at_yahoo.com)
Date: 03/12/04


Date: Fri, 12 Mar 2004 07:24:52 -0800 (PST)
To: "Randy W. Sims" <RandyS@ThePierianSpring.org>


--- "Randy W. Sims" <RandyS@ThePierianSpring.org>
wrote:
> On 03/12/04 08:18, Stuart White wrote:
> > I like the idea of using split() but decided to
> keep
> > most of my regex and incorporate split on the
> string.
> > So the string: 'Spurs 94, Suns 82' <-and there may
> or
> > may not be a space after the 2.
> > I decided to read up on split(), and then try to
> split
> > it.
>
> The split function consumes (throws away) the part
> of the string that
> matches the regular expression used as the first
> argument, so that it no
> longer appears in the result. For example, let's say
> that
>
> $line = 'Spurs 94, Suns 82';
>
> if we use split like
>
> @result = split /,/, $line;
>
> I.e. if we split $line on comma, then result will
> contain:
>
> $result[0] = 'Spurs 94'
> $result[1] = ' Suns 82'
>
> Notice that the combination of the two elements
> would produce the
> original string without the comma. In particular,
> notice that a space
> remains in front of the second element. If we want
> to remove spaces on
> either side of the comma, we can add it to the regex
> like:
>
> @result = split /\s*,\s*/, $line;
>
> which will give us
>
> $result[0] = 'Spurs 94'
> $result[1] = 'Suns 82'
>
> Now we can take each of the results and split on a
> space (or any number
> of spaces) to process each teams score:
>
> foreach my $teamscore (@result) {
> my ($team,$score) = split /\s+/, $teamscore;
> print "Team: $team, Score: $score\n";
> }
>
> This would take each of the two elements in @result
> from above in turn
> and split on one-or-more-spaces, so that the first
> iteration of the loop
> would print:
>
> Team: Spurs, Score: 94
>
> and the second iteration will produce:
>
> Team: Suns, Score: 82
>
> Does that help clarify the way split works?
>

Wow, yeah that helps a lot.
Here's a question: If if had:
$line = 'Spurs 94, Suns 82, Heat 99, Magic 74'
 and then did a split on comma and comma's surrounding
spaces:
@result = split (/\s*,\s*/, $line);
then @result would look like this, right?
@result[0] = 'Spurs 94'
@result[1] = 'Suns 82'
@result[2] = 'Heat 99'
@result[3] = 'Magic 74'

If I wanted to split on the numbers as well, why
doesn't this work:
@result = split (/\s*\d*,\s*\d*/, $line);

I just had a thought, it have to look more like:
@result = split (/(\s*|\d*),\s*\d*/, $line);

I'm confusing myself, but when I get home, I'll try
out what you've shown me. That might be the way to do
it.

> Randy.

__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster
http://search.yahoo.com



Relevant Pages

  • Re: regular expression help
    ... Basically because if you remove everything that is optional in the regex below you end up with an empty regex: ... So the regex engine will try to match on every character in the string: ... , comma doesn't match, but the nothingness in front of it does. ... A quote followed by any sequence of characters that is not a quote, ...
    (microsoft.public.dotnet.framework)
  • Re: regular expression help
    ... regex below you end up with an empty regex: ... , comma doesn't match, but the nothingness behind it does. ... string strValue = pGroup.Value; ... A quote followed by any sequence of characters that is not a quote, ...
    (microsoft.public.dotnet.framework)
  • Re: regular expression help
    ... Basically because if you remove everything that is optional in the regex ... So the regex engine will try to match on every character in the string: ... , comma doesn't match, but the nothingness behind it does. ... A quote followed by any sequence of characters that is not a quote, ...
    (microsoft.public.dotnet.framework)
  • Re: help with a regex and greediness
    ... > most of my regex and incorporate split on the string. ... I.e. if we split $line on comma, ... so that the first iteration of the loop ...
    (perl.beginners)
  • Re: help with a regex and greediness
    ... most of my regex and incorporate split on the string. ... I think it has to do with the comma not being present ... after 'Suns' so I modified the splitto look like ... Do you Yahoo!? ...
    (perl.beginners)