railgun Posted January 15, 2010 Share Posted January 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/188583-help-with-look-ahead-assertion/ Share on other sites More sharing options...
cags Posted January 15, 2010 Share Posted January 15, 2010 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). Quote Link to comment https://forums.phpfreaks.com/topic/188583-help-with-look-ahead-assertion/#findComment-995646 Share on other sites More sharing options...
railgun Posted January 15, 2010 Author Share Posted January 15, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/188583-help-with-look-ahead-assertion/#findComment-995710 Share on other sites More sharing options...
cags Posted January 15, 2010 Share Posted January 15, 2010 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' Quote Link to comment https://forums.phpfreaks.com/topic/188583-help-with-look-ahead-assertion/#findComment-995743 Share on other sites More sharing options...
railgun Posted January 16, 2010 Author Share Posted January 16, 2010 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/188583-help-with-look-ahead-assertion/#findComment-995898 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.