Regex on If statement

Hello, I’m evaluating filenames and I’m hitting a regex (i think t hat should help) issue.

The file names can be structred like…

12_dec_2012
12-dec-2012
12 - dec - 2012
12 _ dec _ 2012
12 dec 2012
12 December 2012

I think this would work, but was hoping someone could help me with regex that could do the same thing.

if($file -like "*_dec_*" `
-or $file -like "*-dec*" `
-or $file -like "*- dec -*" `
-or $file -like "*_ dec _*" `
-or $file -like "* dec *" `
-or $file -like "* december *")
{
do stuff
}

Any help would be greatly appreciated

You probably don’t need to do all that.

if ($file -like ‘dec’) {}

Should do it. And, BTW, -like is not a regular expression operator. It’s a wildcard operator. -match is the regular expression operator.

I cant have -like dec sadly because some files are named like…‘13415 Decision’

Hi,
Like Don said, the operator to use regex is -match. See example below.

$file = "12_dec_2012"
If ($file -match "[_|-|- |_ | ][Dd]ec[_|-| -|_ | |ember]") {
    "matched"
}

$file = "13415 Decision"
If ($file -match "[_|-|- |_ | ][Dd]ec[_|-| -|_ | |ember]") {
    "not matched"
}

Results:

matched
not matched

If each file ends with a year, this should work.

If ($file -match ".*dec.*\d{4}"){Do Stuff}

doesnt seem to match just ‘$file = “december”’

I should have noted that some files are just the month names

"12_dec_2012","12-dec-2012","12 – dec – 2012","12 _ dec _ 2012","12 dec 2012","12 December 2012","December","13415 Decision" |
ForEach-Object {
    $file = $_
    If ($file -match "[_|\-|\- |_ | ]?[Dd]ec[_|\-| \-|_ | |ember]") {
        "$_ - matched"
    }else{
        "$_ not matched"
    }
}

Results:

12_dec_2012 - matched
12-dec-2012 - matched
12 – dec – 2012 - matched
12 _ dec _ 2012 - matched
12 dec 2012 - matched
12 December 2012 - matched
December - matched
13415 Decision not matched

Thanks Curtis, you’re always a great help and really appreciated on these forums!