Reputation: 71
I'm trying to write a small script that prompts users for their email address. The value of this email gets passed to a function which will replace their email suffix such as .net, .com, .edu to .org. I have the following code, but it's not replacing the suffix. Any help in this matter would be appreciated. Thank you.
cls
$mail = Read-Host "Please enter your email address"
Function Email ($change)
{
$modified=$change.Replace(".{3}[A-Za-z]", ".org")
$modified
}
Email $mail
Upvotes: 1
Views: 1581
Reputation: 301157
You are using String.Replace
which doesn't work with regex.
Also, your regex doesn't do what you want it to do.
.
is a special character in regex and you want to escape it. Also the {3}
should be after the character class.
Try the below:
Function Email ($change)
{
$modified=$change -replace "\.[a-zA-Z]{3}$", ".org"
$modified
}
The $
indicates end of string.
PS: If you are not validating input ( through regex match, you can as well use substring to replace to org)
Upvotes: 2