We help IT Professionals succeed at work.

Regular Expressions

Azurewave
Azurewave asked
on
Medium Priority
313 Views
Last Modified: 2010-04-21
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

Comment
Watch Question

b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT

Commented:
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

Author

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.

Author

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.
b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
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
b0lsc0ttIT Manager
CERTIFIED EXPERT
Commented:
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

Author

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?

Author

Commented:
nvm, ignore my last reply i think i figured out the issue.

Give me a bit, and I'll report back with news.

Author

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.
CERTIFIED EXPERT
Commented:
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

Author

Commented:
Yep that was it, thanks guys.
b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
CERTIFIED EXPERT

Commented:
Thanks for the question and the points.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.