Replacing a sub-string with wildcard

Welcome Forums General PowerShell Q&A Replacing a sub-string with wildcard

Viewing 2 reply threads
  • Author
    Posts
    • #255410
      Participant
      Topics: 12
      Replies: 17
      Points: 56
      Rank: Member

      Hi,

      I have a string like “F:\Backup”  I need to replace “F:\” with nothing.  But instead of “F” it could be some other drive, like “D:\Backup”, so I need a wildcard.  Something like:
      $BackupFolder = 'F:\Backup'
      $NewFolder = $BackupFolder -replace "?:\" , ""

      Any ideas?

      Thanks.

       

       

    • #255416
      Participant
      Topics: 1
      Replies: 95
      Points: 435
      Helping Hand
      Rank: Contributor

      Since -replace used regex, you will need to pattern match and backslash scape special regex characters (backslash is special here) that need to be handled literally. See regex for explanation

    • #255419
      js
      Participant
      Topics: 32
      Replies: 852
      Points: 2,779
      Helping Hand
      Rank: Community Hero

      Or “.” in regex means any character. You can leave off the 2nd arg if it’s null. https://regex101.com is a good site to try out patterns.

      • This reply was modified 4 months, 1 week ago by js.
      • This reply was modified 4 months, 1 week ago by js.
Viewing 2 reply threads
  • The topic ‘Replacing a sub-string with wildcard’ is closed to new replies.