Garethp Posted June 8, 2009 Share Posted June 8, 2009 Ok, so this is the code I have so far if(!eregi("Filename ([0-9]?[0-9]?[0-9]).mkv[^.part]", $file)) Basically, I want eregi to check if the Filename ends with .part, and if it does, then it doesn't match. How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/ Share on other sites More sharing options...
Adam Posted June 8, 2009 Share Posted June 8, 2009 check if the Filename ends with .part, and if it does, then it doesn't match. That doesn't make sense. But, the regex to match .part on the end of a string/filename would look like: if(!eregi("\.part$", $file)) It's possible there's syntax problems with that regex when used with eregi as I don't use it very much, I personally prefer using preg_match: if (!preg_match('/\.part$/', $file)) Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-851401 Share on other sites More sharing options...
Garethp Posted June 8, 2009 Author Share Posted June 8, 2009 What I meant, is that with if(eregi("Filename ([0-9]?[0-9]?[0-9]).mkv", $file)) It'll match Filename 001.mkv AND Filename 001.mkv.part I only want it to match Filename 001.mkv, NOT Filename 001.mkv.part Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-851408 Share on other sites More sharing options...
Adam Posted June 8, 2009 Share Posted June 8, 2009 Pardon me I see now. Replace this: [^.part] With this: (?!\.part)$ Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-851409 Share on other sites More sharing options...
nrg_alpha Posted June 8, 2009 Share Posted June 8, 2009 My advice would be to ditch ereg, as this will be depreciated as of PHP 5.3.0 and not even included within the core as of version 6. Learn PCRE instead.. Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-851579 Share on other sites More sharing options...
.josh Posted June 9, 2009 Share Posted June 9, 2009 If you are doing a simple string comparison, it would be more efficient to use a built-in string comparison function, like this: if (substr($file,-5) == ".part") { // file ends in .part, do something } else { // file does not end in .part, do something } Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-851984 Share on other sites More sharing options...
Garethp Posted June 9, 2009 Author Share Posted June 9, 2009 Pardon me I see now. Replace this: [^.part] With this: (?!\.part)$ Thanks, I'll give that a try, but I hate to use and run, so can you explain it to me? I understand that ? is to make the () optional, but what does the ! and $ do? Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-852162 Share on other sites More sharing options...
Adam Posted June 9, 2009 Share Posted June 9, 2009 If you are doing a simple string comparison, it would be more efficient to use a built-in string comparison function, like this: if (substr($file,-5) == ".part") { // file ends in .part, do something } else { // file does not end in .part, do something } I was giving him the wrong answer at first, look at the regex in his first post; there's more to it. Did you manage to get it working? I just noticed you also have .mkv within the regex, this would need to be \.mkv - as you need to escape the dot with a blackslash to use it literally. '?!' as far as I understand means like 'does not equal this'. My regex terminology isn't great! Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-852168 Share on other sites More sharing options...
Garethp Posted June 9, 2009 Author Share Posted June 9, 2009 No, I didn't, I got this error message, and it didn't match anything Warning: eregi() [function.eregi]: REG_BADRPT in /home/garethp/www/Wiki.php on line 104 Every variation of the regex I try either only matches it if it ends in .part of matches nothing Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-852219 Share on other sites More sharing options...
Garethp Posted June 9, 2009 Author Share Posted June 9, 2009 I just figured it out "One Piece ([0-9]?[0-9]?[0-9])\.mkv$" Makes sure it doesn't match anything that has anything after the .mkv Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-852236 Share on other sites More sharing options...
Adam Posted June 9, 2009 Share Posted June 9, 2009 This would do effectively the same thing: "One Piece ([0-9])\.mkv$" No, I didn't, I got this error message, and it didn't match anything Warning: eregi() [function.eregi]: REG_BADRPT in /home/garethp/www/Wiki.php on line 104 Every variation of the regex I try either only matches it if it ends in .part of matches nothing I barely ever use eregi() so I don't really know what that error message is talking about. How do you have the code setup exactly? Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-852255 Share on other sites More sharing options...
Garethp Posted June 9, 2009 Author Share Posted June 9, 2009 I've attached the code. My input is a Folder. The desired input is "One Piece xxx.mkv" xxx being numbers. It pulls from the wikipedia page that lists all One Piece episodes, creates an array, matches the One Piece episode to the right name and number, then renames it. I blocked out the renaming part for testing purposes That's what you get what you present a coder with a really repetitive task. I had 100 Episodes to rename [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-852258 Share on other sites More sharing options...
.josh Posted June 9, 2009 Share Posted June 9, 2009 (?!...) is a negative lookahead. It means look ahead of this thing and only match if it does not equal this. I honestly don't know why you guys keep going on with the regex. You both admit to being in the dark on it. Blind leading the blind. If all you are wanting to do is make sure it ends in .mkv and for some reason you insist on doing it with regex, this would be the most efficient pattern: if (preg_match('~.*mkv$~',$file)) { // file is good, do something } else { // file is bad, do something } But as I mentioned before, this does not even require regex. The substr solution is still faster and simpler. Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-852299 Share on other sites More sharing options...
Adam Posted June 9, 2009 Share Posted June 9, 2009 A'hum.. The desired input is "One Piece xxx.mkv" xxx being numbers. Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-852302 Share on other sites More sharing options...
.josh Posted June 9, 2009 Share Posted June 9, 2009 Touché I guess I should have actually read the last post. Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-852307 Share on other sites More sharing options...
Garethp Posted June 9, 2009 Author Share Posted June 9, 2009 I have it working, but now I'm really curious on how to make sure that the string does not match if it has a certain string in a certain place. Quote Link to comment https://forums.phpfreaks.com/topic/161336-solved-eregi-unless-it-ends-with-part/#findComment-852310 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.