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

Hi guys , how do you ignore the rest of line once you find what you looking for in my example :
Here:\: => M:\p_sold-cr_q00457655_EFLT
I want to only get the first word in this line , in this case it is Here and ignore the rest : this seems to work but , I am sure there is a cleaner way :
$word =~ s/\:\\\:\s+\=\>\s+\w+\:\w*\\\w*//;
thanks

Replies are listed 'Best First'.
Re: matching in a line
by fireartist (Chaplain) on Aug 01, 2002 at 20:04 UTC
    if ( $word =~ /^([a-zA-Z]+)/ ) { $word = $1; }
    the ^ matches the start of the string
    the () assigns whatever it matches between them to $1

    update the above matches any of [a-zA-Z] at the start of the string
    If you want to match any character before the first : then do
    if ( $word =~ /^([^:]+)/ ) { $word = $1; }

    the [^:] means match anything except a :
Re: matching in a line (without destruction)
by BigLug (Chaplain) on Aug 02, 2002 at 02:38 UTC
    Your suggestion, and others, destroy the initial value. This might not matter, but if you want to keep your initial value use:
    ($firstword) = $word =~ /^(.*?):/;
    This leaves $word eq 'Here:\: => M:\p_sold-cr_q00457655_EFLT' and sets $firstword eq 'Here';
Re: matching in a line
by Jaap (Curate) on Aug 01, 2002 at 19:59 UTC
    How about $word =~ s/\:.*$//;