I have a Powershell script that will parse the content of a txt file, and use regular expression to get rid of things not needed.
My issue is that I would like to convert the small script to be in C#. I am having some issues with how regex works in C# and what my limitations are.
Here is the script:
$x = (Get-Location).Path + "\MasterFile.txt" $file = Get-ChildItem "$x" $pathOfx = $file.DirectoryName $dataExtractedFolder = "$pathOfx\TestFolder" New-Item -Path $dataExtractedFolder -ItemType directory -Value $_ -Force [string](cat "$x") | Foreach-Object {$_ -replace "\(in\)", ""} | Set-Content -path "$dataExtractedFolder\Master Trace Data.txt" #This will extract only the data in the selected Master File that starts with a "12 43" and ends with "12 44" $src = [IO.File]::ReadAllText("$dataExtractedFolder\Master Trace Data.txt") $pattern ='(?<Key>(TEMP\s*:\s*12\s*4[34]))\s*(?<Num>(\b[0-9A-F]{2}\s+)+)' [Regex]::Matches($src, $pattern) | % { [Regex]::Replace($_.Groups['Key'].Value, 'TEMP\s:\s*', '') + ' ' + [Regex]::Replace($_.Groups['Num'].Value, '\s+', ' ') }|set-content "$dataExtractedFolder\Master Trace Data.txt"
The file I am parsing has a lot of items I do not need. The script will also strip the file of "(in)" and replace it with an empty string. I am only looking for a particular group of text blocks but can I use the same regular expression as the $pattern in C# doing something like this:
System.Text.RegularExpressions.Regex g = new System.Text.RegularExpressions.Regex(@"(?<Key>(TEMP\s*:\s*12\s*E[34]))\s*(?<Num>(\b[0-9A-F]{2}\s+)+)");
Then Can I parse my data each line by line using the System.Text.RegularExpressions.Regex.IsMatch(s, sPattern);
I would basically like to convert the script to C# but I am lost on how.
Kind Regards