Slug has asked for the wisdom of the Perl Monks concerning the following question:

*SOLVED*

I have a problem with my Perl coursework. Hope someone can help.

I need a regular expression which accepts a filename as a argument.
The conditions for the file name are;

* No greater than 8 characters in length
* No file extension allowed
* At least 3 characters
* Must start with a letter, all though digits are permitted for characters other than the first again.

Here is my incomplete failed attempt. :(

while ($FileStr !~ m/(^[a-zA-Z])\w{2,5}/) {<br><br> print ("Please input a valid filename:");<br> $FileStr = <STDIN>;<br> chomp ($FileStr);<br> $x = length($FileStr);<br> }

Replies are listed 'Best First'.
Re: Regular Expression help
by ikegami (Patriarch) on Jan 07, 2008 at 17:27 UTC
    Another way of reading the specs is:
    It must start (yup, ^ will do)
    with a letter (yup, [a-zA-Z] is the way to go),
    followed by 2 to 7 letters or digits (check out {m,n}),
    followed by the end of the string (\z).

    You don't even need to call length.

    By the way, enclose code in <c>...</c> tags on PerlMonks. It'll handle formatting and escaping characters such as &<>[] for you.

    Update: I wrote "0 to 7 letters or digits" where I meant to write "2 to 7 letters or digits". Fixed. (Maybe I was checking if you were paying attention ;) )

Re: Regular Expression help
by locked_user sundialsvc4 (Abbot) on Jan 07, 2008 at 19:12 UTC

    Tsk... tsk! If this is your Perl homework, how do you expect to master debugging?

    Obviously this is an exercise in creating regular-expressions, and getting those right is truly one of the “essential skills” that is required every single day when you are using this language. If you short-change your debugging and algorithm-refinement task by calling upon the willingness of Monks, you're going to regret it soon enough.

    Even though Monks are out here as a willing and able resource, if you hope to become one someday (not to mention passing a written test where you will be expected to correctly solve a problem such as this manually), you're going to have to master not only the devising of regular expressions but also their debugging. You are going to have to have had to go through variations of this exercise so frequently that it has become “second nature.”

    I repeat:   this is an essential skill. Go through the forest, young padewan, not around it.

Re: Regular Expression help
by CountZero (Bishop) on Jan 07, 2008 at 20:05 UTC
    What do you think of: /^[[:alpha:]][[:alnum:]]{2,7}$/;

    The first character must be alfabetic, followed by 2 to 7 alfanumeric characters. It uses POSIX character classes ([:alpha:] for alfabetic; [:alnum:] for alfanumeric) within the [   ] class tags.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Regular Expression help
by Slug (Acolyte) on Jan 07, 2008 at 17:32 UTC
    Thanks that works nicely now.
    while ($FileStr !~ m/^([a-zA-Z])\w{0,7}\z/){ }

      Try your code with a, ab, abc and abcd. I think your written specification says that of these, only abcd should be accepted, but your regular expression currently accepts all four.

      almost works... I'd try a a filename under three characters before turning it in ;)

                      - Ant
                      - Some of my best work - (1 2 3)

      Furthermore, \w is wrong. It allows characters that shouldn't be allowed. (Specifically, "_")
Re: Regular Expression help
by Anonymous Monk on Jan 07, 2008 at 21:51 UTC
    #!/usr/bin/perl -w use strict; print "Enter filename, CTRL+C to break: "; while (<>) { chomp; if (/^[a-z][a-z0-9]{2,7}$/i) { print "Filename OK\n"; } else { print "Filename sucks, input a better one!\n"; } }
Re: SOLVED Regular Expression help
by bot403 (Beadle) on Jan 08, 2008 at 17:53 UTC
    I found a tool called The Regex Coach to be very helpful as you can see your string being matched in realtime as you tweak the expression. Then, when you have the expression you can tweak the input string and see if it still matches.