Powershell string -Replace unexpected results

This topic contains 7 replies, has 5 voices, and was last updated by  Rich B 2 years, 1 month ago.

  • Author
    Posts
  • #25496

    Stephen Yeadon
    Participant

    I have google around this but have been unsuccessful in finding an answer. The following code gives unexpected results. I would expect the string "some string with a question mark in it" to be returned. But instead the original string is returned. The question mark seems to be causing issues. It doesn't seem to be acting like a regex wild card or any other sort of wild card. Is this expected behavior and if so how do you escape a question mark so I can search for a string containing a question mark

    "some string w?th a question mark in it" -replace 'w?th', 'with'
    
  • #25506

    Wilfredo Perez
    Participant

    You can do something like this

    $str = "some string w?th a question mark in it"
    ($str).Replace( "w?th", "with")
    

    or you can use
    $Str = gc C:\Test\Test.txt <– Read from the text file ($str).Replace( "w?th", "with") Output: some string with a question mark in it

  • #25507

    Dave Wyatt
    Moderator

    PowerShell's -replace operator uses regular expressions. In this case, you'd just need to escape that question mark with a backslash, because the question mark is a special character in regex:

    "some string w?th a question mark in it" -replace 'w\?th', 'with'
    
  • #25509

    Stephen Yeadon
    Participant

    Thanks every one. I had been trying to escape the question mark with a back tick

  • #29537

    Rich B
    Participant

    How does this work if we are replacing using variables, the contents of which are a string that may or may not contain a question mark?

    For example:

    $line1 = $lineReadInFromATextFileWhichMayOrMayNotContainAQuestionMark
    # $line1 = "myWsdl=http://server/path?wsdl"
    $pos = $line1.IndexOf("=")
    $str1 = $line1.Substring($pos+1)
    # $str1 = "http://server/path?wsdl"
    $str2 = $generatedStringToReplaceStr1
    # $str2 = "http://newserver/newpath?wsdl"
    $line1 = $line1 -replace $str1, $str2
    # $line1 = "myWsdl=http://newserver/newpath?wsdl"

    I'd prefer to do using Powershell "-replace" because I also need to use the power of "-match" with these variables:

    if (somehash.Keys -match $str1) { ...
    

    How can this be achieved?

  • #29542

    Dave Wyatt
    Moderator

    You can use [regex]::Escape($str1) to inject the backslashes where needed.

  • #29570

    Bob McCoy
    Participant

    Do you really care about the word match or do you just want to get rid of the question mark(s)?

    "some s?tring w?th a questio?n mark in it" -replace '\?', ''
    
  • #29659

    Rich B
    Participant

    Thanks Dave, that worked.

    I've tested and I can now do the following to get the correct results:

    $line1 = $line1 -replace [regex]::Escape($str1),$str2
    if ($somehash.Keys -match [regex]::Escape($str1)) {...
    

You must be logged in to reply to this topic.