1

Given a block of arbitrary text enclosed by specific tags, I would like to replace the whole chunk with something else (in the example, "BANANA")

$newvar = $oldvar -replace "<!-- URL -->(*.)<!-- END -->","BANANA"

Is there a mode in PS regex to not require escaping and can the syntax then be as simple as this to achieve the replacement?

UPDATE: I understand now that it should be .*, not *., but still no dice. The match covers multiple lines, if that adds complexity to the regex or requires other options.

1
  • 2
    are you sure it is (*.) and not (.*) Commented Jun 19, 2014 at 5:15

1 Answer 1

3

It looks to me like you have the .* in reverse (*.). Apart from that, try:

$newvar = $oldvar -creplace '(?s)<!-- URL -->.*?<!-- END -->', 'BANANA'
  • In response to your comments, I have made the .*? lazy so it will not "overmatch" (for details on lazy vs. greedy, see the reference section)
  • Also in reference to your comments, the (?s) activates DOTALL mode, allowing the .*? to match across multiple lines.

Reference

The Many Degrees of Regex Greed

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

4 Comments

Thanks. -replace "<!-- URL -->","BANANA", with replace or creplace both replace the initial tag, so at least I know now that nothing needs escaping there. However, it is unable to match the whole inner contents. I should add that the contents span multiple lines, perhaps that is it. (.*) does not seem to be working...
That did it, merci beaucoup! (giving you the trophy in T minus 3 minutes ;)
Glad to hear it! Okay, I'm counting down with you, but also trying to hold my breath at the same time. :)
oops, hope you're still alive! My mental timer is also buggy :P

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.