[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] TPJ One-Liner #39



On Tue, Jul 06, 1999 at 11:30:29AM +0200, patrik.grip-jansson@vv.se wrote:
> Patrik Grip-Jansson wrote;
> > perl -e '$/="";$,=" ";print <STDIN>=~/(\b[A-Z]\S*?\b)/g"
> 
> Oops, of course it should end with a ' and not a "!
> 
> Or why not rewrite it (I blatantly "stole" the idea of using  $" for " "
> from Adam Rice's solution);
> 
> perl -e'($,,$/)=$";print<>=~/(\b[A-Z]\w*\b)/g'
> 
> Shorter, slightly more obfuscated, maybe slightly funnier (and that's what
> it's all about, I guess...)  It's still one character longer than Adam's
> solution! Darned! :-)
> 

I don't know that reading the entire file into memory at once is an
improvement.  :)


Anyway, you found what I thought was the biggest problem with this
one-liner: the regex!

/(\b[A-Z]\S*?\b)/

The use of \S*? is quite silly.  This matches a capital letter, and then 0
or more non-whitespace characters up to the first word break.  Of course,
the first word break will be right before the next non-word character (or
end of string).

/(\b[A-Z]\w*\b)/

Replace \S*? with \w*, and the regex matches the same way, but more
efficiently.  But there's still one more change that can be made.

/(\b[A-Z]\w*)/

\w* will already match up to the next word break, so the second \b was
redundant.


My crusade to end useless use of non-greedy matching continues!

Ronald

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe