Re: help with a regex and greediness

From: Randy W. Sims (RandyS_at_ThePierianSpring.org)
Date: 03/12/04


Date: Fri, 12 Mar 2004 09:49:44 -0500
To: Stuart White <poovite@yahoo.com>

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?

Randy.



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
    ... > string. ... > original string without the comma. ... > either side of the comma, we can add it to the regex ... Do you Yahoo!? ...
    (perl.beginners)
  • Re: Fastest way to search a string for the occurance of a word??
    ... but the OP's question was what's the "Fastest way to search a string ... in all the tests I did here, the Regex was by far superior. ... However, of course, if you've got new regular expressions all ... Sure - but just that extra Match object could be relevant if the search ...
    (microsoft.public.dotnet.languages.csharp)