none
Replace accented using powershell RRS feed

  • Question

  • Hi All

    I have a script which is currently replacing accented and German characters to normal characters. I have been provided few more characters and i was trying to understand if in a single replace command i could convert characters which sound similar to normal characters. I tried adding more than one argument in replace command and it threw an error.

    e.g.

    À, Á, Ã, à, á, ã To A, a

    Function getSamAccountName
     {
     Param($Firstname, $Lastname)
     
     $FirstName = $FirstName -replace '\s*',''
     $LastName = $LastName -replace '\s*',''
     $ProposedName = "$Firstname.$Lastname"
     $ProposedName = (Get-Culture).TextInfo.ToTitleCase($ProposedName)
     $ProposedName = $ProposedName -replace "-|'"
     $ProposedName = $ProposedName.Replace("é","e")
     $ProposedName = $ProposedName.Replace("à","a")
     $ProposedName = $ProposedName.Replace("è","e")
     $ProposedName = $ProposedName.Replace("ù","u")
     $ProposedName = $ProposedName.Replace("â","a")
     $ProposedName = $ProposedName.Replace("ê","e")
     $ProposedName = $ProposedName.Replace("î","i")
     $ProposedName = $ProposedName.Replace("à","a")
     $ProposedName = $ProposedName.Replace("ô","o")
     $ProposedName = $ProposedName.Replace("û","u")
     $ProposedName = $ProposedName.Replace("ç","c")
     $ProposedName = $ProposedName.Replace("ë","e")
     $ProposedName = $ProposedName.Replace("ï","i")
     $ProposedName = $ProposedName.Replace("ü","u")
     $ProposedName = $ProposedName.Replace("œ","oe")
     $ProposedName = $ProposedName.Replace("ä","ae")
     $ProposedName = $ProposedName.Replace("ö","oe")
     $ProposedName = $ProposedName.Replace("ü","ue")
     $ProposedName = $ProposedName.Replace("Ä","ae")
     $ProposedName = $ProposedName.Replace("Ö","oe")
     $ProposedName = $ProposedName.Replace("Ü","ue")
     $ProposedName = $ProposedName.Replace("ß","ss")
     $ProposedName = $ProposedName.Replace("ó","o")
     $ProposedName = $ProposedName.Replace("í","i")
     $ProposedName = $ProposedName.Replace("Á","A")
     $ProposedName = $ProposedName.Replace("À","A")
    
    
     Return $ProposedName
    }


    Justin

    Wednesday, March 13, 2019 7:04 AM

Answers

  • I don't know if it's even possible but you could make your code a little bit  easier to read/maintain like this  ...
    $String = 'Übellaunige Östereicher rühren zähflüßigen Brei'
    
    $source = @'
    "special","regular"
    "œ","oe"
    "ä","ae"
    "ö","oe"
    "ü","ue"
    "Ä","Ae"
    "Ö","Oe"
    "Ü","Ue"
    "ß","ss"
    '@
    
    $dict = ConvertFrom-Csv -Delimiter ',' -InputObject $source
    
    foreach ($element in $dict) {
        $String = $String -creplace $element.special, $element.regular
    }
    $String
    
    This way you could even "outsource" the charachter pairs to an external CSV file.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by JS2206 Wednesday, March 13, 2019 12:58 PM
    Wednesday, March 13, 2019 12:45 PM

All replies

  • I don't know if it's even possible but you could make your code a little bit  easier to read/maintain like this  ...
    $String = 'Übellaunige Östereicher rühren zähflüßigen Brei'
    
    $source = @'
    "special","regular"
    "œ","oe"
    "ä","ae"
    "ö","oe"
    "ü","ue"
    "Ä","Ae"
    "Ö","Oe"
    "Ü","Ue"
    "ß","ss"
    '@
    
    $dict = ConvertFrom-Csv -Delimiter ',' -InputObject $source
    
    foreach ($element in $dict) {
        $String = $String -creplace $element.special, $element.regular
    }
    $String
    
    This way you could even "outsource" the charachter pairs to an external CSV file.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by JS2206 Wednesday, March 13, 2019 12:58 PM
    Wednesday, March 13, 2019 12:45 PM
  • Thank you.

    Justin

    Wednesday, March 13, 2019 12:58 PM