none
Using -match in an IF statement that is used in a foreach loop RRS feed

  • Question

  • Hello Everyone,

    I have a script that is working, but I have a behavior that I cannot explain. This script asks will count the number of times a word is used inside a text file. The following script works.  However, if I change the order of the match statement I get an error. I have not been able to explain the difference even with a couple hours of research.  See the second code block for the changed version.  All I did was change ($w -match $wordsearch) to ($wordsearch -match $w). When I do this I get the the error

    parsing "prepare) " - Too many )'s.
    At C:\Users\Administrator\Desktop\story-count.ps1:8 char:4
    + if(($wordsearch) -match ($w)){

    If anyone can help me understand this I would greatly appreciate it!

    Thanks!

    $filepath = read-host "Please type a file path"
    $wordsearch = read-host "Please enter a word to search for"
    $content = get-content $filepath -Delim ' '
    $count = 0
    
    
    foreach($w in $content){
    if($w -match $wordsearch ){
        $count = $count + 1
        }   
    }
    
    Write-Host "The word $wordsearch was counted $count times."

    Non-working code.

    $filepath = read-host "Please type a file path"
    $wordsearch = read-host "Please enter a word to search for"
    $content = get-content $filepath -Delim ' '
    $count = 0
    
    
    foreach($w in $content){
    if($wordsearch -match $w ){
        $count = $count + 1
        }   
    }
    
    Write-Host "The word $wordsearch was counted $count times."

    Tuesday, February 11, 2014 2:27 AM

Answers

  • To safely use this code, you can escape your search term:

    $filepath = read-host "Please type a file path"
    $wordsearch = read-host "Please enter a word to search for"
    $content = get-content $filepath -Delim ' '
    $count = 0
    
    
    foreach($w in $content){
        if ($w -match [regex]::Escape($wordsearch))
        {
            $count++
        }   
    }
    
    Write-Host "The word $wordsearch was counted $count times."

    You can't reverse the order of the operands with the match operator; the string to be searched always goes on the left, and the regex pattern on the right (where the pattern must be found within the string.)  Your logic would go from this:

    Does this line contain this word?

    to this:

    Does this entire line consist of some combination of characters from this word?

    (for example, if the line was "at" and the word you were searching for was "match", the reversed logic would be true, which is weird.)

    • Marked as answer by SiliconRon Thursday, February 13, 2014 4:34 PM
    Tuesday, February 11, 2014 4:02 AM

All replies

  • -match is a regex.  The characters in the right side are special.  You cannot send illegal patterns.  Done the other way there is not chance of an error.

    See: HELP about_Regular_Expressions


    ¯\_(ツ)_/¯

    Tuesday, February 11, 2014 2:41 AM
  • To safely use this code, you can escape your search term:

    $filepath = read-host "Please type a file path"
    $wordsearch = read-host "Please enter a word to search for"
    $content = get-content $filepath -Delim ' '
    $count = 0
    
    
    foreach($w in $content){
        if ($w -match [regex]::Escape($wordsearch))
        {
            $count++
        }   
    }
    
    Write-Host "The word $wordsearch was counted $count times."

    You can't reverse the order of the operands with the match operator; the string to be searched always goes on the left, and the regex pattern on the right (where the pattern must be found within the string.)  Your logic would go from this:

    Does this line contain this word?

    to this:

    Does this entire line consist of some combination of characters from this word?

    (for example, if the line was "at" and the word you were searching for was "match", the reversed logic would be true, which is weird.)

    • Marked as answer by SiliconRon Thursday, February 13, 2014 4:34 PM
    Tuesday, February 11, 2014 4:02 AM
  • Well it makes complete sense now!  Things seem so simple once you have the answer. :D  I did not even consider that -match is a regex. :\ Thank you both!

    Thursday, February 13, 2014 4:33 PM