|
Posted by dummy on April 29, 2008, 1:28 am
Please log in for more thread options On Mon, 28 Apr 2008 08:50:53 GMT, "A. Sinan Unur"
>dummy@phony.info wrote in
>
>> On Sun, 27 Apr 2008 23:46:31 GMT, "A. Sinan Unur"
>>
>>>1h4nVnZ2dnUVZ_vOdnZ2d@giganews.com:
>>>
>>>> I needed a regex that says "either at the start of a line, OR
>>>> preceded by some whitespace".
>>>
>>>The only difference between this criterion and "preceded by
>>>whitespace" can occur at the beginning of the string. Therefore:
>>>
>>>#!/usr/bin/perl
>>>
>>>my $x = <<EOSTR;
>>>Test1 Test2
>>> Test3 Test4 Test5
>>> Test6
>>>Test7 Test8
>>>Test9
>>> Test0a
>>>
>>>EOSTR
>>>
>>>print "$1\n" while $x =~ /(?:\A|\s+)(\S+)/g ;
>>
>> On my XP machine that produces:
>> Test1
>> Test2
>> Test3
>> Test4
>> Test5
>> Test6
>> Test7
>> Test8
>> Test9
>> Test0a
>>
>> But this:
>>
>> use strict; use warnings;
>> while (<DATA>) {
>> print "$1\n" if /^\s*(\S+)(?:\s|$)/;
>> }
>> __DATA__
>> Test1 Test2
>> Test3 Test4 Test5
>> Test6
>> Test7 Test8
>> Test9
>> Test0a
>>
>> Gives:
>> Test1
>> Test3
>> Test6
>> Test7
>> Test9
>> Test0a
>>
>> which I think is better?
>
>How can that be better?
>
>Read the OP's criterion again:
>
>>>> I needed a regex that says "either at the start of a line, OR
>>>> preceded by some whitespace".
>
>Yours misses Test2, Test4, Test5 and Test8 which are all preceded by
>whitespace.
>
>Sinan
OOPS!
|