windowspowershellunicodearabicright-to-left

Change every word that contains Arabic letters To Reversed


Since Arabic text is automatically reversed in PowerShell, I had to reverse-flip it to make it readable.
For example, the text 'مرحبا' , will look 'ابحرم'

I can change them one by one, but I have difficulty with many words at once.

function ReverseText {
    Return ( -Join ($Args.ToCharArray() | Sort {(--$script:i)}) )
}
$AJoin  = 'ابتثجحخدذرزسشصضطظعغفقكلمنهوي'
$AMatch = $AJoin.ToCharArray() -join '|'

$data = @(
"1. The verb for the male singular in Arabic is?"
"   a) 'تفعل'" 
"   b) 'تفعلان'"  
"   c) 'يفعلون'"  
"   d) 'يفعل'"
"2. 'تلميذ' is the mufrad form of the word?"
"   a) 'تلاميذ'"
"   b) 'تلميذات'"  
"   c) 'تلميذان'" 
"   d) 'تلميذين'"
)

ReverseText 'بيتان'
$Newdata = $data | Foreach {
    If ($_.Split("'") -match $AMatch) {
        'configuous to continue'
    }
}
$Newdata; pause

From the script, i can reserve one word 'بيتان' with command ReverseText 'بيتان'.

From $data, I want to change every Arabic word into reverse letter order, then save it in $Newdata. It's only 2 out of 50 taken from a .txt file, as example.
I'm not capable enough to do it yet.

I appreciate any helps. Thanks.


Solution

  • Do the following in order to selectively reverse runs of Arabic characters in your input strings:

    $data | ForEach-Object {
      [regex]::Replace($_, 
        '\p{IsArabic}{2,}',
        { param($m) [Array]::Reverse(($chars = $m.Value.ToCharArray())); [string]::new($chars) }
      )
    }
    
    $data -replace 
      '\p{IsArabic}{2,}', 
      { [Array]::Reverse(($chars = $_.Value.ToCharArray())); [string]::new($chars) }