I did some testing on my Perl 5.10 Win XP system. At first all appeared to be ok. Then I tried to replicate the output of the poster and found that I could do it if the second match failed.

It appears that if match fails, $1 remains the same as it was, i.e. $1 is only valid if match succeeds.

I experimented with  ${type} vs just $type and found that both worked on my Perl version. I am wondering if something about this on Perl 5.8.0 is somehow different? That this second match is not succeeding and old $1 is still there? I would suggest trying some of my experiments below and see what happens on the OP's system. I think the second match is failing for some reason and the "old $1" is getting printed.

#!/usr/bin/perl -w print "get some string: "; ($string = <STDIN>); #note: chomp not necessary, $ should count \n as #end of string. should work with or without chomp. $string =~ m/\/([[:alnum:]]+)_.*\.(.+)$/; print "dollar 1: $1\n"; $type = $2; print "type: $type\n"; #something weird here.... $string =~ m/(.+)\.BBB$/; #get some string: /xxxx/yyyy/ZZZ_xxxx.CCC #dollar 1: ZZZ #type: CCC #dollar 1:ZZZ print "match failed\n" unless $string =~ m/(.+)\.BBB$/; #$string =~ m/(.+)\.${type}$/; #ok #$string =~ m/(.+)\.$type$/; #ok also print "dollar 1:$1\n"; exit 0; __END__ This is with the match failed code: C:\TEMP>regextest.pl get some string: /xxxx/yyyy/ZZZ_xxxx.CCC dollar 1: ZZZ type: CCC match failed dollar 1:ZZZ This is from: #$string =~ m/(.+)\.${type}$/; #ok #$string =~ m/(.+)\.$type$/; #ok also C:\TEMP>regextest.pl get some string: /xxxx/yyyy/ZZZ_xxxx.CCC dollar 1: ZZZ type: CCC dollar 1:/xxxx/yyyy/ZZZ_xxxx C:\TEMP>perl -v This is perl, v5.10.0 built for MSWin32-x86-multi-thread (with 5 registered patches, see perl -V for more detail)

In reply to Re: perl pattern match for end of string using STDIN and chomp by Marshall
in thread perl pattern match for end of string using STDIN and chomp by cbolcato

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.