Jump to content

[SOLVED] eregi Unless it ends with .part


Garethp

Recommended Posts

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?

Link to comment
Share on other sites

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))

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

(?!...) 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. 

 

Link to comment
Share on other sites

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.