This is working well.So I am getting $str="himanshu.txt"

Not possible, $2 is always undefined.

#!/usr/bin/perl -- use strict; use warnings; my $str="himanshu.txt^@"; if($str =~ /(\w+)/) { $str = "$1.$2"; } print "$str\n" __END__ Use of uninitialized value in concatenation (.) or string at - line 6. himanshu.
What will the regular expression?
use re 'debug'; to see, ex
#!/usr/bin/perl -- use strict; use warnings; use re 'debug'; my $str="himanshu.txt^@"; if($str =~ /(\w+)/) { $str = "$1.$2"; } print "$str\n" __END__ Compiling REx `(\w+)' size 7 Got 60 bytes for offset annotations. first at 4 1: OPEN1(3) 3: PLUS(5) 4: ALNUM(0) 5: CLOSE1(7) 7: END(0) stclass "ALNUM" plus minlen 1 Offsets: [7] 1[1] 0[0] 4[1] 2[2] 5[1] 0[0] 6[0] Matching REx "(\w+)" against "himanshu.txt^@" Matching stclass "ALNUM" against "himanshu.txt^@" Setting an EVAL scope, savestack=5 0 <> <himanshu.txt> | 1: OPEN1 0 <> <himanshu.txt> | 3: PLUS ALNUM can match 8 times out of 2147483647.. +. Setting an EVAL scope, savestack=5 8 <manshu> <.txt^@> | 5: CLOSE1 8 <manshu> <.txt^@> | 7: END Match successful! Use of uninitialized value in concatenation (.) or string at test.pl l +ine 7. himanshu. Freeing REx: `"(\\w+)"'

In reply to Re: pattern matching by Anonymous Monk
in thread pattern matching by himanshu.padmanabhi

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.