0

I have a textfile with CRFL line endings, I read the whole file with $c = Get-Content -Raw file.txt the file contains e.g.

    exec Add a, b, c
    exec Rem e, f, g

I try to replace it with my regex $c = $c -replace '(?m)^([ \t])(@exec@)([ \t]+)([a-zA-Z0-9_-]+)(.)$' '$1call$3$4($5)' This doesn't work and I don't know why but to get it working I need to run $c = $c -replace '(?m)^([ \t])(@exec@)([ \t]+)([a-zA-Z0-9_-]+)(.)\r\n?$' '$1call$3$4($5)' and the result is

    call Add(a, b, c
)
    exec Rem(e, f, g
)

with a LF after the ) bracket. I would expect to get

    call Add(a, b, c)
    exec Rem(e, f, g)

with CRLFs

What's wrong with PowerShell and $ and CRLF? Can anybody tell me how to get the correct results? Thanks.

2 Answers 2

1

I found the "bug" and a solution. The $ at the end is not working with MS regex

(?m)^...\r\n

This works

Sign up to request clarification or add additional context in comments.

Comments

0

I could not get your regex to match anything but this more generic replace should do the trick:

-replace  '(?m)^(\s*)([^\s]*)(\s*)([^\s]*)(\s*)(.*)$', '$1call$3$4$5($6)'

2 Comments

Thanks, I will check my example. I've tried your example and it produces the wrong output as I described with only a LF instead or CRLF
If you skip the -raw option in get-content powershell makes any line endings CRLF. Seems that your inputfile might have a LF CRLF mix.

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.