f***y 发帖数: 98 | 1 #!/usr/bin/perl
$line = "one two three...";
print "$line\n";
$line =~ s/^([^ ]+) +([^ ]+)/$2 $1/;
print "$line\n"; | w*****n 发帖数: 94 | 2
[^ ] stands for one char which is not space.
the third line reads like: find a token (one or more non space chars),
one or more spaces, and another token, and change them to the second
token ($2), space and the first token ($1).
+ stands for one or more, () are used to capture, everything matched
in the first () is stored in $1, $2 for the second (). BTW, the ^ at
the beginning of RE stands for match beginning of the string ($line).
get a perl book or "man perlre" for details.
【在 f***y 的大作中提到】 : #!/usr/bin/perl : $line = "one two three..."; : print "$line\n"; : $line =~ s/^([^ ]+) +([^ ]+)/$2 $1/; : print "$line\n";
|
|