Not only is the above badly formatted, it occured to me to test additional possibilities; so (with apologies), revised, cleaned up, etc. :
#!C:/perl/bin -w my @testitems = qw( aaa_gjh1_dfgdf_0009 aaa_gjh_0000 aaa_gjh1_DFgdf_0009 aaa_fdsfs_000 aaa_sdf_jdsh_01111 aaa_gjh1_dfgdf_0009 aaa_gjhi0000 aaa_gjh1_df<:df_0009 aaa_gjh_0000 aaa_ABCDEFGHI123456789ZYXW_1111 aaa_ABCDEFGHI123456789ZYXWVUT_1111); # next to last: 23 before the last underlin +e # last element: more than 24 intermediate c +hars for my $item(@testitems) { if ( $item =~ m/ ^aaa_ # starts with "aaa_" [_A-Za-z0-9]{1,23} # test next 1,23 chars: underline, alphas or d +ecimal_nums (?<=\D) # Pos_LookBehind, Last char BEFORE last 4 (may + be the 24th) # can't be a digit else we may find 5 di +gits at the end \d{4}$ # has ONLY - emphasis mine - four digits at th +e end /x ) { print "MATCHES: $item\n"; } else { print "NO match: $item\n"; } } print "done\n"; =head1 OUTPUT: MATCHES: aaa_gjh1_dfgdf_0009 MATCHES: aaa_gjh_0000 MATCHES: aaa_gjh1_DFgdf_0009 # caps are alpha; OP m +ay want only lc NO match: aaa_fdsfs_000 # only 3 digits at end + of string # (contrary to sp +ec) NO match: aaa_sdf_jdsh_01111 # 5 digits at end of s +tring # (contrary to sp +ec) MATCHES: aaa_gjh1_dfgdf_0009 MATCHES: aaa_gjhi0000 # no underscore before + final 4 digits # (ok per spec) NO match: aaa_gjh1_df<:df_0009 # includes symbol and +punct # (contrary to sp +ec) MATCHES: aaa_gjh_0000 MATCHES: aaa_ABCDEFGHI123456789ZYXW_1111 # < 24 chars before fi +nal 4 digits # (ok per spec) NO match: aaa_ABCDEFGHI123456789ZYXWVUT_1111 # > 24 chars before fi +nal 4 digits # (contrary to s +pec) done =cut

But see grinder's elegant (and earlier) node, below!


In reply to Re^3: Number of digits at the end of the string by ww
in thread Number of digits at the end of the string by isha

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.