Jump to content

Recommended Posts

As you can tell from the expression, I am making sure that the filename of an image is in a particular format.  The trouble I'm having is with the look ahead assertion and trying to make sure that a period cannot follow another period. Then, after conquering that, I will also do the same for the other allowables.

 

preg_match('/^[A-Z]{3}+\-+\d{1,2}+\_+[a-zA-Z0-9]+\.jpg$/', $filenameString);

 

Thanks for your expertise.

Link to comment
https://forums.phpfreaks.com/topic/188583-help-with-look-ahead-assertion/
Share on other sites

Since you have a fixed filetype, if you simply don't allow the fullstop/period as a character, then job done, no need for a lookahead assertion. Generally speaking with Regular Expressions, it's impossible to give advice without having a list of actual requirements, afterall we can't assume your pattern is correct, especially as I can't tell what it's supposed to do. The plus following the {} seems dubious at best. I'm sure what your after isn't too difficult, but unfortunately without knowing exactly what your after it would be fairly pointless giving any pointers (since anything may need to be changed based on something you haven't currently mentioned).

Thanks for the reply.  I didn't realize that I had inadvertently taken the period, hyphen, and underscore out of the expression but that is the case. And the + following the {} is just inexperience.  My aim is to allow the above mentioned characters as part of the filename but just to NOT allow two of them consecutively.  So, with that in mind, and to try to further my regex education, when you get some spare time, tell me how in the blue blazes do you keep two consecutive periods or hyphens or underscores out of the filename?

My computer died and I'm currently just on a laptop that has no facilities setup for me to play around so I really can't be certain this is going to work, but I'd have thought you'd want to use a negative look behind assertion. As I say this is purely off the top of my head any may not work, but hopefully I'll have my new computer up and running tomorrow or the day after.

 

'#(??:(?<![./-])[./-])|[a-z0-9])+#i'

Sorry about your computer.  Everyone needs to have 3 or 4 spares.  Thanks again for your help.  I think I have muddled through it and come up with a workable, however un-elegant solution. I'll include it for posterity.  My goal was to have a filename in an exact format, but to NOT have any two non alphanumeric characters consecutive. So far the below seems to be working fine in PHP.

 

$str = "ABC-12_e-f3.jpg";

 

if ((preg_match('/^[A-Z]{3}\-\d{1,2}\_[A-Za-z0-9._-]+\.jpg$/', $str)) && (!preg_match('/[._-](?=[._-])/', $str))) {

print "Good Filename";

} else {

print "Bad Filename";

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.