1

Hi!

  • Is the "-replace" Parameter bounded to the cmdlet "Get-Content"? through googling i found it in many different contexts
  • Why is the -replace Parameter not shown in the official Documentation? Get-Content Microsoft

Issue:

The text apporaches like this:

olor sit amet, consetetur sadipscing elitr, sed diam nonumy LoremIpsum-648648sdfsd

I want to replace the substring via Regex from "LoremIpsum-xxx" to "Loremipsum"

This is my first Try:

(Get-Content "C:\File.cmd") -replace "[regex]::matches("LoremIpsum-(.*?)")" | Out-File -encoding ASCII C:\File.cmd
1

1 Answer 1

4

-replace is not a Get-Content parameter, it is an operator that allows to use a regular expression to find a specified pattern and then either remove or replace the matched text with another.

In your case, you can use

(Get-Content "C:\File.cmd") -replace '(?<=\bLoremIpsum)-\w+' | Out-File -encoding ASCII C:\File.cmd

The (?<=\bLoremIpsum)-\w+ regex matches a hyphen (-) and one or more word chars (\w+) that are immediately preceded with a LoremIpsum as a whole word (\b is a word boundary).

Note you may replace \w+ with \S+ if you want to remove any one or more non-whitespace chars after LoremIpsum.

Alternatively, you can use

(Get-Content "C:\File.cmd") -replace '\b(LoremIpsum)-\w+', '$1' | Out-File -encoding ASCII C:\File.cmd

Here, LoremIpsum is captured into a capturing group (with ID 1 since it is the first capturing group in the regex), and the replacement is now $1, the replacement backreference referring to Group 1 value.

See the regex demo #1 and this regex demo.

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

6 Comments

Thanks for your answer sir - Why cant i simply google with keywords "powershell","-replace" and get the satisfying results? how do i approach a satisfying google search? @Wiktor Stribiżew
@NoErrorNoCry I used powershell replace regex documentation and the top result brought me to the necessary page.
@NoErrorNoCry By placing "-" in front of a keyword, it actually excludes this keyword from search! If you want to search for it literally, surround it in double quotation marks. Try this: powershell "-replace"
Got another problem: Sometimes the "-"-Hyphen emerges a few times: LoremIpsum-648648sdfsd-skldfjsdo54-545dfd6-sldfj the pattern '(?<=\bLoremIpsum)-\w+' produces following output: LoremIpsum-skldfjsdo54-545dfd6-sldfj but i want to remove everything
@NoErrorNoCry No problem, see my "Note you may replace \w+ with \S+ if you want to remove any one or more non-whitespace chars after LoremIpsum." Use -replace '(?<=\bLoremIpsum)-\S+'. Right, - excludes a word from search in Google search, hence, I did not use - before replace in my Google search.
|

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.