0

Why replace is not working? Here is the code

$TestString = "<css>1</css><PredefinedValidator>9DE32F03C2734FFCB2D681FF6283FE88</PredefinedValidator><RegexPattern>^[\da-zA-Z\s+()\-']+$</RegexPattern><item>2</item>"
$NewString =  $TestString  -replace "<PredefinedValidator>9DE32F03C2734FFCB2D681FF6283FE88</PredefinedValidator><RegexPattern>^[\da-zA-Z\s+()\-']+$</RegexPattern>","<PredefinedValidator>9DE32F03C2734FFCB2D681FF6283FE88</PredefinedValidator><RegexPattern>^[0-9]+$</RegexPattern>"
write-host  $NewString

Is there something I am doing wrong?

Any suggestions would be appreciated.

Thanks in advance

1
  • If this is about making changes in an XML file, you shouldnýt use textual replace methods. There are many examples on how to do this, but if you show us the xml we can help you with that. Commented Mar 11, 2021 at 13:39

1 Answer 1

1

The replace operator will replace a string matching a regex pattern. From the looks of it, you are attempting to replace a literal string with another literal string and both of them happen to also have a regex pattern in them. Use the Replace method instead

$TestString = "<css>1</css><PredefinedValidator>9DE32F03C2734FFCB2D681FF6283FE88</PredefinedValidator><RegexPattern>^[\da-zA-Z\s+()\-']+$</RegexPattern><item>2</item>"
$NewString =  $TestString.Replace("<PredefinedValidator>9DE32F03C2734FFCB2D681FF6283FE88</PredefinedValidator><RegexPattern>^[\da-zA-Z\s+()\-']+$</RegexPattern>","<PredefinedValidator>9DE32F03C2734FFCB2D681FF6283FE88</PredefinedValidator><RegexPattern>^[0-9]+$</RegexPattern>")
write-host  $NewString
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.