Regex replace

Welcome Forums General PowerShell Q&A Regex replace

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
1 year, 6 months ago.

  • Author
    Posts
  • #72215

    Participant
    Points: 57
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 259
    Helping Hand
    Rank: Contributor

    -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

    Participant
    Points: 57
    Rank: Member

    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

      Participant
      Points: 259
      Helping Hand
      Rank: Contributor

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

The topic ‘Regex replace’ is closed to new replies.