0

I'm trying to edit a file with powershell, the thing is that I need all the information between two strings, this is currently what I have

$testText = Get-Content C:\Users\Administrator\Documents\test1\test1.txt

$pattern = '(?<=\DocMasterBOL>).+?(?=\DocFWImport>)'

[regex]::Matches($testText, $pattern).Value

where "DocMasterBOL>" is the beginningof the string and "DocFWImport>" the end of it

Do somebody knows how to do the same but adding the last part of the string? At the moment is adding results as I need them but without the end of the string "DocFWImport>"

  • 1
    If I understand your need correctly, to include it in your results, you just need to remove the `(?=` and `)` from around the end string. – TheMadTechnician Jun 01 '23 at 18:43
  • Additionally, note that `\D` is an escape sequence that matches any non-digit character; use just `D` to match that letter literally. – mklement0 Jun 01 '23 at 18:51
  • 1
    It looks like you're trying to extract text from an xml file - you might be better off parsing it using the ```[xml]``` type accelerator instead and getting the content you're interested in by walking the object model or via an xpath query. You won't need to worry about the nitty gritty detail of the xml file then - for example if it contains whitespace in the tags like ```< DocMasterBOL >... DocMasterBOL >``` or if the document content has xml entities like ```&```... – mclayton Jun 01 '23 at 19:17
  • @TheMadTechnician thank you, that worked, Now I want to save the results in the same file that I'm reading, I tried with Set-Content C:\Users\Administrator\Documents\test1\test1.txt – Eduardo Escamilla Salazar Jun 02 '23 at 00:05
  • If you're looking to add content to a file use `Add-Content` instead of `Set-Content` – TheMadTechnician Jun 02 '23 at 17:21
  • @TheMadTechnician I want to overwrite the same file I'm reading – Eduardo Escamilla Salazar Jun 02 '23 at 17:58

0 Answers0