#!C:/perl/bin -w =head I want a regular expression that checks that a string has only 4 digit +s at the end & starts with "aaa_", inbetween string can have digits,a +lphabets and underscore{1,24} . ... Please provide a regexp that says: Match for "aaa_gjh1_dfgdf_0009","aaa_gjh_0000" No Match for "aaa_fdsfs_000", "aaa_sdf_jdsh_01111". i.e. allows ONLY 4 digits at the end of a string =cut my @testitems = qw( aaa_gjh1_dfgdf_0009 aaa_gjh_0000 aaa_gjh1_DFgdf_0009 aaa_gjh_0000 aaa_fdsfs_000 aaa_sdf_jdsh_01111 aaa_gjh1_dfgdf_0009 aaa_gjh1_df<:df_0009 aaa_gjh_0000 aaa_fdsfs_000 ); for my $item(@testitems) { if ( $item =~ m/ ^aaa_ # starts with "aaa_" [_A-Za-z0-9]{1,23} # test next 1,23 chars: underline, alpha or dec +imal_nums (?<=\D) # Positive_Lookbehind, Last char BEFORE last 4 # (potentially, the 24th) # can't be a digit else we may find 5 digit +s at the end \d{4}$ # has ONLY - emphasis mine - four digits at the + end /x ) { print "Matches: $item\n"; } else { print "\tNo match: $item\n"; } } print "done\n"; =head1 OUTPUT: Matches: aaa_gjh1_dfgdf_0009 Matches: aaa_gjh_0000 Matches: aaa_gjh1_DFgdf_0009 Matches: aaa_gjh_0000 No match: aaa_fdsfs_000 No match: aaa_sdf_jdsh_01111 Matches: aaa_gjh1_dfgdf_0009 No match: aaa_gjh1_df<:df_0009 Matches: aaa_gjh_0000 No match: aaa_fdsfs_000 done =cut
In reply to Re^2: Number of digits at the end of the string
by ww
in thread Number of digits at the end of the string
by isha
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |