Author Posts

June 22, 2016 at 2:05 pm

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

  • This topic was modified 2 years, 3 months ago by  ertuu85.

June 22, 2016 at 2:09 pm

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.

June 22, 2016 at 2:41 pm

I cant have -like *dec* sadly because some files are named like....'13415 Decision'

June 22, 2016 at 2:56 pm

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

June 22, 2016 at 3:23 pm

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

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

June 22, 2016 at 3:39 pm

doesnt seem to match just '$file = "december"'

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

  • This reply was modified 2 years, 3 months ago by  ertuu85.

June 22, 2016 at 5:41 pm

"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
  • This reply was modified 2 years, 3 months ago by  Curtis Smith.

June 22, 2016 at 6:33 pm

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