splitOnce has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am doing the follwing :
if ( ($newLine =~ /$/)[0] eq /(\w+)$/ ) { print " the line $newLine\n"; }
to see weather my line end with a word mix with character and digits ,, a line would look like this :
yty~uyuoo~iu99~##uyt~hu/dwo9
that line is a one I would like to print b/c it countins a word at the end "dwo9" , however if it was somthing like @@ or a digit by it self or ? mark then I wont print. The pice of code I am doing seems to catch everything which is not what i want . peace

Replies are listed 'Best First'.
Re: regex end of line
by japhy (Canon) on Jul 30, 2002 at 19:40 UTC
    Your code is quite broken. It's not doing what you're trying to do at all.
    if ( ($newLine =~ /$/)[0] # always true, returns 1 eq /(\w+)$/ # returns 1 if $_ ends in word, "" if not ) { ... }
    You just want to say: if ($newLine =~ /\w$/) { ... }. That checks to see if $newLine ends in a word character. Maybe you even want /\w{2}$/ instead, to ensure at least two word characters.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: regex end of line
by Cine (Friar) on Jul 30, 2002 at 19:36 UTC
    If all you want to know is if your $newLine ends in \w chars and not a single digit you can use
    if ($newline =~ /\w+$/ && $newline !~ /(?<=\W)[0-9]$/)...

    T I M T O W T D I
      thanks
Re: regex end of line
by DamnDirtyApe (Curate) on Jul 30, 2002 at 19:33 UTC

    How about:

    if ( $newline =~ /\/[a-zA-Z0-9]+$/ ) { # Some stuff... }

    Update: Added backslash at beginning of pattern.


    _______________
    D a m n D i r t y A p e
    Home Node | Email