Regex on If statement

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of ertuu85 ertuu85 5 months, 2 weeks ago.

  • Author
    Posts
  • #44337
    Profile photo of ertuu85
    ertuu85
    Participant

    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 5 months, 2 weeks ago by Profile photo of ertuu85 ertuu85.
  • #44340
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #44348
    Profile photo of ertuu85
    ertuu85
    Participant

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

  • #44350
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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
    
  • #44356
    Profile photo of random commandline
    random commandline
    Participant

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

    If ($file -match ".*dec.*\d{4}"){Do Stuff}
    
  • #44360
    Profile photo of ertuu85
    ertuu85
    Participant

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

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

    • This reply was modified 5 months, 2 weeks ago by Profile photo of ertuu85 ertuu85.
  • #44381
    Profile photo of Curtis Smith
    Curtis Smith
    Participant
    "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 5 months, 2 weeks ago by Profile photo of Curtis Smith Curtis Smith.
  • #44426
    Profile photo of ertuu85
    ertuu85
    Participant

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

You must be logged in to reply to this topic.