2

I have a string with HTML content.

I want to find few characters and replace with some other string.

For eg:

$Content = "This is my string. For more information, goto http://www.example.com/searchpage.htm. You will get the required information here. You can also get information at http://www.example.com/searchpage1.htm. Do not replace sample.htm.  Do not replace sample1.htm."

Wherever I find the text example.com, I need to replace with example1.com and the same URL contains .htm which should be made .html

If the URL does not contain example.com, I dont want to replace .htm Below is some part of code, which I could achieve to replace example.com But I am unable to replace .htm part in the same URL.

I dont want to replace .htm all over the main content. I just want to replace .htm wherever I replace example.com

$find = "http://www.example.com/"

$newContent = "http://www.example1.com/"

$replacedContent =  $Content -replace $strFind,$strReplace

I want to know how to replace a multiple parts of text in a long string.

Please suggest!

1
  • -replace '\bexample\.com\b','example1.com' Commented Jan 30, 2017 at 12:26

1 Answer 1

1

Probably not the safest way (in case the URL doesn't contain .htm) but could do it for you:

$Content -replace 'http://www\.example\.com(.+?)\.html?', 'http://www.example1.com$1.html'
Sign up to request clarification or add additional context in comments.

4 Comments

Thank You, but the URL contains .htm. I need to modify the URL which contains example as well as htm to example1 and .htm
you mean to .html. my snippet does exactly that - just try it.
Unfortunately, this is not working for me. I am not sure where I am going wrong. Please guide me
Suppose the orginal string contains "www.example.com/search-page/searchpage.htm" I need to change it to "www.example1.com/search-content/searchpage.html"

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.