'$ quick question

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of turbo mcp turbo mcp 2 years, 1 month ago.

  • Author
    Posts
  • #19765
    Profile photo of turbo mcp
    turbo mcp
    Participant

    Hi
    I have a script that's supposed to change a value in a file on multiple servers
    it works and all, but as I would like to understand and learn what each part does(99.9 % i do)
    I cannot understand what this little $ sign does and why do I need it:
    -replace 'MaxActiveMovesPerSourceMDB = "5"$','MaxActiveMovesPerSourceMDB = "50"' `

    why do I need the $ sign before I close the value i want to replace with a ' ?

    replace 'MaxActiveMovesPerSourceMDB = "5"$',

    Thanks

  • #19767
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    PowerShell's -replace operator uses regular expressions for its first argument. In a regex, the $ character means to only match the pattern at the end of the string (or at the end of a line, depending on how you're using it.) There's an about_Regular_Expressions help file in PowerShell that can help you get started with that.

  • #19769
    Profile photo of turbo mcp
    turbo mcp
    Participant

    Thanks for a blazing fast reply:)
    I only saw one documentation of this '$ sign but I wasn't sure if it even fits what I see here cause I got some value enclosed with 'value1' to replace with another value 'value2'
    in my case I think im replacing/matching whatever is inside these ' ' so that's why I don't understand why this $ is needed:)
    but maybe its just faster this way it match "end characters instead of the whole value?
    I wonder what "end character mean anyway:)? 1?3? or just from the end to start whatever matches?
    sorry for the many questions

  • #19772
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Well, say that your string varialbe contained this:

    $string = "cat dog cat dog cat dog"
    

    You can see the difference between using the $ in the pattern or not:

    $string -replace 'dog', 'zebra' # This replaces all 3 instances of "dog"
    
    $string -replace 'dog$', 'zebra'  # This replaces only the word "dog" at the very end of the string.
    
  • #19778
    Profile photo of turbo mcp
    turbo mcp
    Participant

    understood
    Thanks a million

You must be logged in to reply to this topic.