0

The input text from allacts.txt is:

institutionId: blah blah,name: 1st thing,laestabNo: blah blah   institutionId: blah blah,name: 2nd thing,laestabNo: blah blah, [lots more things]   institutionId: blah blah,name: last thing,laestabNo: blah blah

The desired output is:

1st thing,laestabNo: blah blah  2nd thing,laestabNo: blah blah, [lots more things]  last thing,laestabNo: blah blah

The command I am using within a powershell script:

Powershell -NoProfile "(Get-Content -Raw .\allacts.txt) -replace 'institutionid.*,name', '' | Out-File -FilePath allacts.txt -Force -Encoding ASCII"

What I actually get is:

last thing,laestabNo:blah blah

And nothing else. What am I doing wrong?

3
  • 3
    in regex, the . means one character. the * means zero or more of the previous character. so you are telling it to replace EVERYTHING from the 1st institutionid to the last ,name. [grin] take a look at "greedy & lazy matches" for how to limit that. Commented Apr 8, 2021 at 19:02
  • Thanks yeah I see that now. Just goes to show a little bit of RTFM sometimes beats hours of head-keyboard-smacking. Commented Apr 9, 2021 at 10:21
  • you are most welcome! glad to have helped a little bit ... [grin] Commented Apr 9, 2021 at 13:20

1 Answer 1

1

Your regex is not correct for your use case. You mean this...

$SomeString = 'institutionId: blah blah,name: 1st thing,laestabNo: blah blah   institutionId: blah blah,name: 2nd thing,laestabNo: blah blah, [lots more things]   institutionId: blah blah,name: last thing,laestabNo: blah blah'
Clear-Host
# RegEx to replace all text string patterns and the text between them
$SomeString -Replace '(institutionId: ).*?(name: )'
# Results
<#
1st thing,laestabNo: blah blah   2nd thing,laestabNo: blah blah, [lots more things]   last thing,laestabNo: blah blah
#>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.