change variable outcome form 1C201234 to IC201234 1 to I

information out variable change to other variable

in the program below we read a PDF file if the variable is 1C###### (# di digits) i want to change it to IC#######

the PDF makes sometimes fault an read a 1 instead of an I

variable is $logistixid

if $logistixid = 1C201630 then change to IC201630

the numbers change every time 1C combination is the same

I don’t program in powershell only VBA bu i hope it is a simpel question

thanks for your help

 

<!–more–>

prepare the pdf

$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $sourcefile

$logistixid = $null

for each page

for($page = 1; $page -le $reader.NumberOfPages; $page++) {

set the page text

$pageText = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader,$page).Split([char]0x000A)

if the page text contains any of the keywords we’re evaluating

$match = [regex]::Match($pageText, $idpattern ).captures
if($match)
{
foreach ($m in $match)
{
$logistixid = $m.value.ToUpper()
break;
}
}
}
$reader.Close()

return $logistixid<!–more–>

Yes, it’s straightforward:

$logistixid = '1C00001'

if ($logistixid -match "^1C\d+") {

    $logistixid = $logistixid.Replace("1C","IC")

}

$logistixid

 

 

thank you for the solution it works

now have solved several scans but i have a new challenge, i can change the first letters and a lot of document can be place in the right map

but the read in PDF gives more problems in the in the 6 digits after IC the PDF will give a O for a 0 (zero) and the 1 is also gives I how can i change that in the same if statement?

So when the PDF give back 1C2OI234 it mist be IC201234

Well the code example I gave won’t trigger if there are letters in the last six digits as the regular expression I gave ^1C\d+” will only match 1C followed by 1 or more numbers.

I would suggest you just use -replace to replace all the O’s or I’s, then change the first two characters in the string to IC.

$logistixid = '1C2OI234'

$logistixid = $logistixid -replace "o","0"
$logistixid = $logistixid -replace "i","1"
$logistixid = $logistixid -replace "^.{2}","IC"

$logistixid

Note that by default the -replace operator is not case sensitive.