hi ive got this segment of code that im struggling with the code does what i want it to do in the sense that if i put in the filename of the file that i have created it works but for a random user the code will not produce the desired result.
heres the code:

this essentially works apart from the {1} & {7} what i want this to do is match any letter or underscore once then accept input of 7 letters\digits or underscore characters and obviously .txt as its a file however if i put in say 12 characters its still saying that its correct which of course is wrong it appears im doing something wrong but what it is i just cant see.

your question is very hard to not only understand but also to read because you seem to not know how to make sentences that other people can read and make sense of so please edit your post and use proper grammar and post an example of the strings you are trying to match and the ones that think should match and ones you think should not match

Apologies for the confusion

Lets try again shall we:

I have this Segment of code: m/^[a-zA-Z\_]{1}[a-zA-Z0-9]{7}(\.txt|\.TXT)$/)

This pattern is to check for a filename. The filename should start with either a letter or underscore character. It should then match a further 7 characters, these can either be letter, number or an underscore character. The last part of the pattern can match either .txt or . TXT .
The problem i am having is that this pattern should only match eight characters(excluding the .txt). If i enter, say for example, a filename 12 characters long, perl is telling me the pattern matches. It should match 8 characters only.
Hope thats easier to understand

Your second charcter class does not include an underscore, but say it does:


since you are using string anchors ^ and $ it will match 8 characters and then the file extension. If your regexp is matching filenames with more than 8 characters followed by the extension you are doing something wrong. But since you did not post samples of the data (like I asked) its hard to say. Instead of [a-zA-Z0-9_] you can use \w which is the exact same thing:


when I run that against some test names it works as expected.

Post all of your code and some sample data.

sorry got it wrong again, right sample code, the file im testing for is named testtext.txt

this is the part of code that the previous regex were included in:


if($#ARGV == -1)
  print("Please enter a filename ");
  $file = <STDIN>;
  $file = $ARGV[0];

      if($file !~ m/^[a-zA-Z\_]{1}[a-zA-Z0-9\_]{7}(\.txt|\.TXT)/) 
      die("Incorrect format!");  

if you need more code to work with let me know but i would think this is all you need, appreciate your help Kevin ADC

Your latest code no longer has the end of string anchor ($) so it could match strings of any length as long as the initial pattern matches. But lets assume your code does have the end of string anchor and run a test:

while (my $file = <DATA>) {
   if( $file !~ m/^[a-zA-Z\_]{1}[a-zA-Z0-9\_]{7}(\.txt|\.TXT)$/){
      print "$file - Incorrect format!\n";  
   else {
      print "$file - correct format!\n";


qwertyuio.txt - Incorrect format!
12wedcfr.TXT - Incorrect format!
asd38_gy.TxT - Incorrect format!
asd38_gy.txt - correct format!
@qwedcvnt.txt - Incorrect format!
asdfgter.txt - correct format!
dgh!cbgt.txt - Incorrect format!
1234rtyuio.TXT - Incorrect format!
a123dfghj.txt - Incorrect format!
aqwertyu.gif - Incorrect format!

looks good to me.

its working great, looks like i just got a bit muddled thanx for making it clearer