Perl Whitespace Reg Expression
Bill Campbell
linux-sxs
Sun Oct 9 10:45:46 PDT 2005
On Sun, Oct 09, 2005, Jorge Almeida wrote:
>On Sat, 8 Oct 2005, Steve Jardine wrote:
>
>> Alright you Perl Heads, here's one for you.
>>
>> I have a line like this:
>>
>>
>> abcdef<1 to many spaces>more text
>>
>>
>> I want to use the splt command so I can get to the "more text" part of the
>> line, but I do not know how many spaces there may be - it could vary. So,
>>
>> @line = split(/ /, $_);
>> $line[1] = ...
>>
>@line = split(/ +/, $_);
Actually split automatically splits on whitespace and also defaults to the
$_ input so this will work:
my @line = split();
In perl regular express ``\s'' is shorthand for whitespace, and ``\S''
shorthand for NOT whitespace so this should work as well:
my @line = split('\s', $_);
FWIW: Python also uses perl-style regular expressions, and the pcre
package provides a grep substitute, ``pcregrep'' that uses them as well.
Bill
--
INTERNET: bill at Celestial.COM Bill Campbell; Celestial Software LLC
UUCP: camco!bill PO Box 820; 6641 E. Mercer Way
FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676
URL: http://www.celestial.com/
The day-to-day travails of the IBM programmer are so amusing to most of
us who are fortunate enough never to have been one -- like watching
Charlie Chaplin trying to cook a shoe.
More information about the Linux-users
mailing list