in reply to japhy blabs about regexes (again)

I haven't tried any benchmarks and I am likely not to, but this little ditty comes to mind:

s!\s*\s$!!

Although, I can also see how it would be slower :)

Replies are listed 'Best First'.
Re: Re: japhy blabs about regexes (again)
by japhy (Canon) on Jul 16, 2001 at 22:43 UTC
    Oh, I was hoping someone would bite. ;)

    That regex is worse. You'd think by breaking \s+ down into \s*\s, you'd get the best of both worlds -- the "1 or more" meaning, and the "trailing character" optimization. No such luck. In fact, the regex does MORE work, because of backtracking. :(

    You see, Perl doesn't even see if the string has ANY trailing whitespace if you do /\s+$/. Meaning it takes a while to fail for a string like "a b c d e f g h i j k l m n o p q r s t u v w x y z". How sad.

    # try this, and be sad :( use re 'debug'; "abc def ghi" =~ /\s+$/; "abc def ghi" =~ /\s*\s$/;
    The output (if you understand it) will upset you. Well, it upset me.

    _____________________________________________________
    Jeff japhy Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;