Nico
Nico

Reputation: 251

Powershell regex are too annoying

I have the following URLs

http://servername:12345/sites/MYSITE1/SUBSITE1
http://servername:543/sites/MYSITE2/SUBSITE1/SUBSITE2

I would like to delete everything before and including /sites/ to give the following output

MYSITE1\SUBSITE1
MYSITE2\SUBSITE1\SUBSITE2

I have tried -replace "*/sites/","" but his doesn't give me the required result.

Upvotes: 0

Views: 189

Answers (1)

Vinoth
Vinoth

Reputation: 1995

.* means any number of character. So it will remove all letters before the sites. Some characters ". ^ $ * + ? { [ ] \ | ( ) “ has special meaning in regex. So we need to mask it to use literally using “\”. Example “/” for the “/”. Try this.

"http://servername:12345/sites/MYSITE1/SUBSITE1" -replace ".*sites\/",""

Upvotes: 4

Related Questions