Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Need 2 Regex's

by BlenderHead (Novice)
on Jul 24, 2010 at 15:33 UTC ( [id://851171]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Need 2 Regex's
by Corion (Patriarch) on Jul 24, 2010 at 15:53 UTC

    See perlretut and perlre. Maybe also just see index.

    This is not a code writing service. We will gladly help you with your code or give you pointers as to where to look.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Need 2 Regex's
by derby (Abbot) on Jul 24, 2010 at 17:18 UTC

    Well ... a regex will really not help you with either problem (if truly identifying jpg/png/gif files and preventing name collisions in a multiprocess environment are your goals).

    A valid jpg/png/gif file does not need one of those extensions and many times I've had users upload a .doc file with a .gif extension (I have no idea why). To properly identify file type you want File::Mimeinfo.

    For #2, you may run into a a classic race condition (especially if you're going through a web server). To ensure non-colliding file names, something like a GUID (Data::GUID) appended to the end of each file (or using File::Basename to append the GUID before the suffix) would work.

    -derby
Re: Need 2 Regex's
by intel (Beadle) on Jul 24, 2010 at 16:52 UTC
    You posted no Perl code to examine in order for us to help you.
    You posted no protocol for 'file uploads' (FTP, SCP, rsync?)
    You posted no OS details, you could be trying to copy windows shares or NFS mounts or, well, who knows?
    and this?:
    Anyone else may avoid wasting everyone else's time, and keep their thoughts to themselves.
    Means that you did not read corion's helpful links to the Perl regex tutorial. You don't need a regex, you need to learn the fundamentals of how regex works.

    This is a very poorly formed question, you should add more information and read the tutorials provided to you.

Re: Need 2 Regex's
by graff (Chancellor) on Jul 24, 2010 at 17:28 UTC
    Your item 1 is so simple, I wonder how you can know anything at all about perl and not know how to do that -- three minutes or less with any man page about regexes would answer the question. The solution in a previous reply should suffice.

    Your item 2 does not strike me as a regex problem. You presumably have a file name from an upload request, and a directory path where the upload should be stored. The "-f" function with a "path/name" arg will tell whether the file name exists in the directory: if ( -f "$path/$file" ). If that's true, you append a number to the file name and check again. If necessary, increment the number, append to the original file name and check again; repeat until "-f" returns false. (UPDATE: derby's suggestions above are much better -- use that instead.)

    It's appalling that you've wasted a lot of time complaining about replies that "waste your time" by asking you to show what you've tried, and after all that, you apparently still have not tried anything yourself -- at least, you haven't given any evidence of having tried. So stop whining and try something. Learn by doing.

    UPDATE: Actually, I'm curious about why you posted your question 1 here in the first place. It seems remarkably similar to another thread that you posted here a year ago. Did you really completely forget the advice given back then, or did you never understand it?

Re: Need 2 Regex's
by sierpinski (Chaplain) on Jul 26, 2010 at 03:35 UTC
    You also apparently forgot that Corion was one of the people who helped you in a previous comment of yours.... I looked at the nodes you wrote, and sadly only a couple had any code in them at all, but apparently you got the help you were looking for then anyway. Now that you didn't get the response you desired, you start with the name calling and disrespect.
Re: Need 2 Regex's
by NetWallah (Canon) on Jul 24, 2010 at 17:31 UTC
    In an attempt to stay on-topic, I offer (untested):

    1 . !~/\.(?:jpg|png|gif)$/ 2. perl -e '$f=shift(); -e $f and do{ my ($r,$d)=$f=~/(.+?)(\d*)$/; +$d++; $NEWFILENAME=qq|$r$d\n|}'
    More <to-the-point> answers may follow, if asked politely.

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: Need 2 Regex's
by pemungkah (Priest) on Jul 26, 2010 at 22:58 UTC
    BTW, just because a user says that the file is a .jpg does not mean it is a .jpg. There are a couple of XSS exploits based on this, because some browsers will look at what they get, figure it out, and do the appropriate thing ("oh look, this Javascript is called .jpg, but it's really a script, so I'll execute it. Oops.")

    To steal from House, "users always lie". Validate the file you get in the upload and process it accordingly instead of trusting that the file is what it says it is. (edit: typo.)

Re: Need 2 Regex's
by suhailck (Friar) on Jul 24, 2010 at 17:00 UTC
    :)
    I agree with Corion,marto and intel.
    Then also, im trying to give possible solution i can think of right now

    1
    perl -le 'foreach $file qw(a.JPG b.exe c.gif d.png e.doc f.xls){ next +if $file !~ /\.(jpg|png|gif)$/i;print $file}'

    2

    perl -le '@arr1=qw(a.jpg a.jpg1 b.jpg);@arr2=qw(a.jpg b.jpg c.jpg);foreach $file1 (@arr2) { foreach $file2 (sort @arr1) { $file1=~s/(\d)?$/($1)?$1+1:1/e if $file1 eq $file2;}print $file1}'


    "Peace to everyone"
    ~suhail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://851171]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found