Regular Expressions

Hey there, well I'm horrible at regular expressions.

Basically I am trying to do the following:

I have 3 video files as an example.  Each file is named slightly differently some files have the format

_clip_
_clip
clip_

looks like
(randomtext)_clip_(randomtext)
(randomtext)_clip(randomtext)
(randomtext)clip_(randomtext)

I would just like to store them appropriately into the array sets that they belong to.  I attached some of the code I am using, but again the regular expressions aren't there because I am really bad at it.
<?php
 
//Example:
/*
 * 
 * bobsgreatadventure_clip1_56k.wmv
 * bobsgreatadventure_clip156k.wmv
 * bobsgreatadventureclip1_56k.wmv
 * 
 */
 
// First look if we have clip in the code
if(strpos($minifile, 'clip'))
{
    // Check to see if it's _clip_
    if(preg_match('_clip_', $minifile))
    {
        $set_one[] = $minifile;
    }
    // Check to see if it's _clip
    elseif (preg_match('_clip', $minifile))
    {
        $set_two[] = $minifile;
    }
    // Check to see if it's clip_
    elseif (preg_match('clip_', $minifile))
    {
        $set_three[] = $minifile;
    }
}
 
?>

Open in new window

LVL 1
AzurewaveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

b0lsc0ttIT ManagerCommented:
Azurewave,

What type of variable is $minifile ?  Does it just have the file name and you are just "testing" for the match?

Try ...

if (preg_match('/_?clip_?/', $minifile, $regs) {
    $match = $regs[0];
} else {
    $match = "";
}

Let me know if you have any questions or need more information.

b0lsc0tt
0
ddrudikCommented:
Azuerwave, how can these files be named?  Your question mentions _clip_ but your example shows _clip1_

Will the files contain other characters  other than _clip_ that you want to test?  Please be as specific as possible since that will cause the regex pattern supplied to change.

In any case, here's some test code that works against your current sample data:
<?php
function checkminifile($minifile) {
  global $set_one,$set_two,$set_three;
  // First look if we have clip in the code
  if(strpos($minifile, 'clip'))
  {
 
    // Check to see if it's _clip_
    if(preg_match('/_clip1_/', $minifile, $temp))
    {
        $set_one[] = $minifile;
    }
    // Check to see if it's _clip
    elseif (preg_match('/_clip1(?!=_)/', $minifile, $temp))
    {
        $set_two[] = $minifile;
    }
    // Check to see if it's clip_
    elseif (preg_match('/(?<!_)clip1_/', $minifile, $temp))
    {
        $set_three[] = $minifile;
    }
  }
}
echo '<pre>';
checkminifile('bobsgreatadventure_clip1_56k.wmv');
echo '$set_one '.print_r($set_one,true).'<br>';
checkminifile('bobsgreatadventure_clip156k.wmv');
echo '$set_two '.print_r($set_two,true).'<br>';
checkminifile('bobsgreatadventureclip1_56k.wmv');
echo '$set_three '.print_r($set_three,true);
?>

Open in new window

0
AzurewaveAuthor Commented:
Ah yes, sorry about that.

The clip is followed by a random number.

_clip(number)_

it's a string, and it would always be something like

_clip(number)_
_clip(number)
clip(number)_

the string before and after "_" could be anything.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

AzurewaveAuthor Commented:
better example

(randomtext)_clip(number)_(randomtext)
(randomtext)_clip(number)(randomtext)
(randomtext)clip(number)_(randomtext)

I completely forgot about the number thank you for pointing that out.
0
b0lsc0ttIT ManagerCommented:
Good catch Ddrudik and that was an important piece of info left out Azurewave.  Thanks for clarifying.

My suggestion would need to be changed to ...

if (preg_match('/_?clip\d+_?/', $minifile, $regs) {
    $match = $regs[0];
} else {
    $match = "";
}

Let me know how that works or if you have a question.

bol
0
b0lsc0ttIT ManagerCommented:
As I reread this I realize my suggestion is over simplified and not what you want.  You want the filename, not just the clip part, and need them organized into groups.  Sorry I overlooked that.

Try ...

{
    // Check to see if it's _clip_
    if(preg_match('/_clip\d+_/', $minifile))
    {
        $set_one[] = $minifile;
    }
    // Check to see if it's _clip
    elseif (preg_match('/_clip\d+/', $minifile))
    {
        $set_two[] = $minifile;
    }
    // Check to see if it's clip_
    elseif (preg_match('/clip_\d+/', $minifile))
    {
        $set_three[] = $minifile;
    }
}

If the step in the If (i.e. $set_NNN[] = $minifile;) is suppose to fill an array then I can help you with that part too.

Let me know if you have a question or need more info.

bol
0
AzurewaveAuthor Commented:
thanks b0lsc0tt, everything seems to be coming into play however

    // Check to see if it's _clip
    elseif (preg_match('/_clip\d+/', $minifile))
    {
        $set_two[] = $minifile;
    }

that part of the code is getting thorugh the filter.

i run it and this
bobsgreatadventure_clip1qt.mpg

goes astray, any ideas?
0
AzurewaveAuthor Commented:
nvm, ignore my last reply i think i figured out the issue.

Give me a bit, and I'll report back with news.
0
AzurewaveAuthor Commented:
alright i found out another isse:

for:

preg_match('/_clip\d+/',

on this, it needs to make sure that the character that follows is a number.

ex: bobsadventure_clip4qt.wmv

I have some files that are
bobsadventure_clipqt.wmv

that I don't want in the system at all thats why they should'nt make it into the arrays.
0
ddrudikCommented:
Azurewave, try this code and report:
<?php
function checkminifile($minifile) {
  global $set_one,$set_two,$set_three,$set_bad;
  // First look if we have clip in the code
  if(strpos($minifile, 'clip'))
  {
 
    // Check to see if it's _clip_
    if(preg_match('/_clip\d+_/', $minifile, $temp))
    {
        $set_one[] = $minifile;
    }
    // Check to see if it's _clip
    elseif (preg_match('/_clip\d+(?!=_)/', $minifile, $temp))
    {
        $set_two[] = $minifile;
    }
    // Check to see if it's clip_
    elseif (preg_match('/(?<!_)clip\d+_/', $minifile, $temp))
    {
        $set_three[] = $minifile;
    }
    else {
        $set_bad[] = $minifile;
    }
  }
}
echo '<pre>';
checkminifile('bobsgreatadventure_clip1_56k.wmv');
checkminifile('bobsgreatadventure_clip156k.wmv');
checkminifile('bobsgreatadventureclip1_56k.wmv');
checkminifile('bobsgreatadventureclip156k.wmv');
echo '$set_one '.print_r($set_one,true).'<br>';
echo '$set_two '.print_r($set_two,true).'<br>';
echo '$set_three '.print_r($set_three,true).'<br>';
echo '$set_bad '.print_r($set_bad,true).'<br>';
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AzurewaveAuthor Commented:
Yep that was it, thanks guys.
0
b0lsc0ttIT ManagerCommented:
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0
ddrudikCommented:
Thanks for the question and the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.