2

I want to replace a simple string "WEEK." (with a dot) in a text file with the string "TEST"

$LOG= "C:\FILE.TXT"
$A= "TEST"
(Get-Content $LOG) | Foreach { $_ -Replace "WEEK.", $A } | Set-Content $LOG;

The problem is that my file has this content:

WEEK_A WEEK.

And when I run my script the result is:

TESTA TEST

and the result that i want is:

WEEK_A TEST

I try with ^ "WEEK." and "^WEEK.$" but it not worked

Can you help me with the regexp? Thanks

====== EDIT ==================

Ok. I try with

$LOG= "C:\FILE.TXT"
$A= "TEST"
(Get-Content $LOG) | Foreach { $_ -Replace "WEEK\.", $A } | Set-Content $LOG;

and seems its works

1
  • Seems you have answered your own question Commented Aug 6, 2013 at 10:49

1 Answer 1

4

The reason why this happened is because you have used pattern WEEK. The dot was a problem: in a regular expression world, the dot means "any character". That's why it was replacing both WEEK_ and WEEK..

When you have added backslash, then the dot was escaped ie. it lost it's special meaning. Thus making it work.

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.