Re: help with a regex and greediness
From: Randy W. Sims (RandyS_at_ThePierianSpring.org)
Date: 03/12/04
- Next message: Christian Stalp: "how to make sure, that the script is running one time?"
- Previous message: R. Joseph Newton: "Re: Reading text file in reverse order"
- In reply to: Stuart White: "Re: help with a regex and greediness"
- Next in thread: Stuart White: "Re: help with a regex and greediness"
- Reply: Stuart White: "Re: help with a regex and greediness"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Christian Stalp: "how to make sure, that the script is running one time?"
- Previous message: R. Joseph Newton: "Re: Reading text file in reverse order"
- In reply to: Stuart White: "Re: help with a regex and greediness"
- Next in thread: Stuart White: "Re: help with a regex and greediness"
- Reply: Stuart White: "Re: help with a regex and greediness"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|