Regex replace

This topic contains 4 replies, has 3 voices, and was last updated by  Olaf Soyk 5 months, 2 weeks ago.

  • Author
    Posts
  • #72215

    kishor
    Participant

    Hi,
    When I type below command the new value is appended to the existing value;
    (Get-Content C:\temp\File.ini) -replace 'IP_1=*', ('IP_1='+$ip) | Set-Content c:\temp\File.Ini

    And when I add a period to the old character value 'IP_1=.*' the old value is replaced totally, wanted to know how does period work here?

    Thanks,
    Kishor

  • #72221

    Chris
    Participant

    Is it trying to dot source the file? I can't be sure, but that's perhaps what its doing, look at

    http://www.computerperformance.co.uk/powershell/powershell_syntax.htm#Operators_

    Its also used for objects, so maybe it's doing something funky? Sorry i couldn't be more help.

  • #72229

    Olaf Soyk
    Participant

    -replace works with regular expressions. A period is the meta charachter for "any charachter" and the star/asterisk (*) is the modifier meaning 0 or any number of occurances of the charachter or meta charachter or group right before this meta charachter.

    What is it, what you're trying to accomplish?

  • #72268

    kishor
    Participant

    Thank you Olaf and Chris.
    I am using the command to modify an IP address in the ini file IP_1 = x.x.x.x to IP_1 = y.y.y.y

    Thanks,
    Kishor

    • #72272

      Olaf Soyk
      Participant

      If you're looking for literal periods with regex you have to "escape" them with a backslash ... like this "\."

You must be logged in to reply to this topic.