in reply to Regex that will append to end of same line

I got it:

my $text = "1..., some words, Dec 13, 2001"; if ($text =~ m/^\1/) { $text =~ m/(.*$)/; print "${1},P"; }

Thanks ;-}

Replies are listed 'Best First'.
Re: Re: Regex that will append to end of same line
by Juerd (Abbot) on Jan 04, 2002 at 01:41 UTC
    Nooooo! :) See Death to dot star to read why .* is bad.
    You can't use \1 there, you should just use 1.
    Please note that /^1/ will also match strings beginning with 123.

    I suggest the following solution:
    $line =~ s/$/,P/ if /^1\D/;


    Which will substitute the end of the line (which is _before_ a newline if there is any) with ,P (and thus "append" it) if the line starts with 1 followed by anything-that-is-not-a-digit.

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: Re: Regex that will append to end of same line
by talexb (Chancellor) on Jan 04, 2002 at 01:37 UTC
    Or, even shorter (and hopefully clearer) ..
    my $text = "1..., some words, Dec 13, 2001"; print "$text,P" if ( $text =~ /^1/ );
    Although you may want \n at the end of the line.

    --t. alex

    "Excellent. Release the hounds." -- Monty Burns.